+ Reply to Thread
Results 1 to 3 of 3

nested Loop

Hybrid View

iturnrocks nested Loop 02-21-2008, 05:36 PM
mikerickson I think that part of your... 02-21-2008, 07:07 PM
iturnrocks Excellent idea, as I hit... 02-21-2008, 07:26 PM
  1. #1
    Forum Contributor
    Join Date
    08-08-2006
    Posts
    203

    nested Loop

    If itemQTY1 = 4 I want this result

    QTY 4
    QTY 3
    QTY 2
    QTY 1

    If itemQTY = 3

    QTY 3
    QTY 2
    QTY 1

    and so on.......


    Here is the idea, but this code doesnt work because of the multiple Do's

    Do Until itemQTY1 = 0
        
        Range("A1").Select
        
        Do
        
        If IsEmpty(ActiveCell) = False Then
        
            ActiveCell.Offset(1, 0).Select
            
        End If
        
        Loop Until IsEmpty(ActiveCell) = True
        
           
        itemQTY1 = textboxQTY1.Value
        
        ActiveCell.Offset(0, 0) = "QTY:"
        
        ActiveCell.Offset(0, 1) = itemQTY1
        
        itemQTY1 = itemQTY1 - 1
        
        Loop

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    I think that part of your problem is that everytime you write one row and decrement itemQTY1, you then go back to the top of the column to start searching for the next cell.
    This kind of construct might be easier

    Range("A1").Select
    Do
        If ActiveCell.Value = vbNullString Then
            ActiveCell.Offset(0, 0) = "QTY:"
            ActiveCell.Offset(0, 1) = itemQTY1
            itemQTY1 = itemQTY1 - 1
        End If
        
        ActiveCell.Offset(1, 0).Select
    
    Loop Until itemQTY1 = 0
    I just realized that this is also a good example to show how to do the same thing without Selecting (i.e. faster). Declaring a range variable, myCell, and moving it as the Select was moving the Active Cell will achieve the same result without Selecting.
    Dim myCell As Range
    
    Set myCell = Range("A1")
    Do
        If myCell.Value = vbNullString Then
            myCell.Offset(0, 0) = "QTY:"
            myCell.Offset(0, 1) = itemQTY1
            itemQTY1 = itemQTY1 - 1
        End If
        
        Set myCell = myCell.Offset(1, 0)
    Loop Until itemQTY1 = itemQTY1 - 1
    Last edited by mikerickson; 02-21-2008 at 07:12 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Contributor
    Join Date
    08-08-2006
    Posts
    203
    Excellent idea, as I hit myself in the head. I feel like such an idiot sometimes. Thank you, I will try it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1