+ Reply to Thread
Results 1 to 2 of 2

Do while loop issue

Hybrid View

  1. #1
    Registered User
    Join Date
    04-29-2008
    Posts
    1

    Question Do while loop issue

    Hi, I am trying to pull out various subtotals from a list terminating with the word "End". My code finds the first instance only although both the counter and the rowpos increment correctly. I am beginning to lose the will to live resolving this
    _________________________________________________________________
    Sub Activities()
    Sheets("Report Paste").Select
    Counter = 1
    RowPos = 10
    Do While Cells(Counter, 1) <> "End"
        If Cells(Counter, 2) = "Total" Then
            Range(Cells(Counter, 1), Cells(Counter, 3)).Select
            Selection.Copy
            Sheets("Weekly Calculator").Select
            Range(Cells(RowPos, 10), Cells(RowPos, 12)).Select
            Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=False
            RowPos = RowPos + 1
        End If
        
        Counter = Counter + 1
    Loop
    
    End Sub
    _________________________________________________________________

    Any assistance with this would be appreciated.
    Last edited by Leith Ross; 04-29-2008 at 10:57 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Licquor,

    This is happening because you don't re-select "Report-Paste" after you have pasted the data. Here is a better method of coding that doesn't involve selecting the ranges or sheets.
    Sub Activities()
     
      Dim Counter As Long
      Dim DstWks As Worksheet
      Dim RowPos As Long
      Dim SrcWks As Worksheet
    
        Set SrcWks = Sheets("Report Paste")
        Set DstWks = Sheets("Weekly Calculator")
    
        Counter = 1
        RowPos = 10
    
          Do While SrcWks.Cells(Counter, 1) <> "End"
            If SrcWks.Cells(Counter, 2) = "Total" Then
               SrcWks.Cells(Counter, 1).Resize(1, 3).Copy Destination:=DstWks.Cells(RowPos, 10)
               RowPos = RowPos + 1
            End If  
            Counter = Counter + 1
          Loop
    
    End Sub
    Sincerely,
    Leith Ross

+ 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