+ Reply to Thread
Results 1 to 22 of 22

Help with a VBA macro ending with a LOOP

Hybrid View

  1. #1
    Registered User
    Join Date
    12-13-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    42

    Help with a VBA macro ending with a LOOP

    Hi Guys

    I have a macro created and it starts on the cell which the user specifies and ends on the cell below. Is there a way in which i can have it looped until the end cell is empty?

    Many Thanks

    Shil



    --------------------------------


    Sub moverec()
    '
    ' moverec Macro
    '
    ' Keyboard Shortcut: Ctrl+m
    '
        ActiveCell.Offset(0, 36).Range("A1:BE1").Select
        Selection.Copy
        Sheets("Bank File").Select
        Range("A1").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1, 0).Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveCell.Offset(-1, 0).Range("A1:D1").Select
        Application.CutCopyMode = False
        Selection.Copy
        ActiveCell.Offset(2, 0).Range("A1").Select
        ActiveSheet.Paste
        Sheets("Payroll Calculatios").Select
        ActiveCell.Offset(1, -36).Range("A1").Select
    End Sub
    Last edited by 5h1l; 07-11-2012 at 11:33 AM. Reason: Use code tags in future.

  2. #2
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Help with a VBA macro ending with a LOOP

    Can you include a small sample of the workbook and a description of what the macro does?

  3. #3
    Registered User
    Join Date
    12-13-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Help with a VBA macro ending with a LOOP

    Please find attached the example.

    WorkSheets:

    Payroll calculations - is the are which calculates the pay for that employee.
    Bank File - Takes the information from the Payroll Calculations and deploys it in the specific method we need.

    The current process is that you press Cntrl + n to prepare the first two rows in bank file.
    Then Clicking on cell A5, press cntrl + m to copy the record from payroll calculations to the bank file. It ends the macro in the cell below you started. You have to keep on pressing cnrtl + m until the macro ends in cell A18 (First empty Cell).

    What i would like to do is to keep the macro on a loop so that it loops until it finds the empty cell.

    I hope this helps.

    Please let me know should you require further information.

    Thanks

    Shil
    Attached Files Attached Files

  4. #4
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Help with a VBA macro ending with a LOOP

    Select a cell and click the button.
    Attached Files Attached Files
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  5. #5
    Registered User
    Join Date
    12-13-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Help with a VBA macro ending with a LOOP

    Hi Japan Dave.

    Your button only takes one line and transfers it. It doesn't loop all the lines?

    Thanks

    Shil

  6. #6
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Help with a VBA macro ending with a LOOP

    See if this is what you are after?
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    12-13-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Help with a VBA macro ending with a LOOP

    No im afraid not Japan Dave. Your file only copies the one row and copies accross the same row.

    I would like the macro to move accross one row, one at a time until it comes to the empty cell.



    Shil

  8. #8
    Registered User
    Join Date
    12-13-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Help with a VBA macro ending with a LOOP

    So here is my current macro code:


    Sub moverec()
    '
    ' moverec Macro
    '
    ' Keyboard Shortcut: Ctrl+m
    '
        ActiveCell.Offset(0, 47).Range("A1:BE1").Select
        Selection.Copy
        Sheets("Bank File").Select
        Range("A1").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1, 0).Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveCell.Offset(-1, 0).Range("A1:D1").Select
        Application.CutCopyMode = False
        Selection.Copy
        ActiveCell.Offset(2, 0).Range("A1").Select
        ActiveSheet.Paste
        Sheets("Payroll Calculatios").Select
        ActiveCell.Offset(1, -47).Range("A1").Select
    End Sub
    After looking online i think i found the code for the loop:


    Sub Test2()
    
          ' Select cell A2, *first line of data*.
          Range("A2").Select
          ' Set Do loop to stop when an empty cell is reached.
          Do Until IsEmpty(ActiveCell)
             ' Insert your code here.
             ' Step down 1 row from present location.
             ActiveCell.Offset(1, 0).Select
          Loop
       End Sub
    The only part where im struggling is how to edit the current code so it includes this second code i found online.

    I hope someone knows the answer.

    Thanks

    Shil
    Last edited by Cutter; 07-16-2012 at 09:47 AM. Reason: Added code tags

  9. #9
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Help with a VBA macro ending with a LOOP

    No it does not. It copies down from the row you select. If you select range("AV5") it will copy all rows from there to the last row in "AV5" into sheet "Bank File".

  10. #10
    Registered User
    Join Date
    12-13-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Help with a VBA macro ending with a LOOP

    When i click on Cell AV5 and click on your button it takes that row and duplicates this all the way down. Look at column 24 for confirmation (in the bank file worksheet). It comes up with Bernadita all the way down. Whereas i'm expecting to see All the other names here.

    Perhaps im running it incorrectly?

  11. #11
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Help with a VBA macro ending with a LOOP

    Or perhaps you are not explaining yourself correctly. Or I am not understanding you correctly?
    My code copies all the values from Range("AV5:CZ5") to Range("AV22:CZ22") into "Bank File". If you want different data copied you are going to need to specify it.

  12. #12
    Registered User
    Join Date
    12-13-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Help with a VBA macro ending with a LOOP

    Ok so should i be in cell AV5 and then click on the button or should i be selecting the whole range and then clicking on the button?

    I have tried both and they give the same outcome. They both replicate the first row (Row 5) downwards and it doesnt take the rows beneath row 5 and copy these accross.

  13. #13
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Help with a VBA macro ending with a LOOP

    You should only need be in AV5.
    And of coarse it will replicate that row, b/c your file has the same data in the following rows until the empty one. Can you not see what is happening? It copies row 5 from column AV, then row 6 , row 7. Your data is exactly the same in these rows. If you don't what the same data, you need to specufy where you what it copied. Or you need to change the data.
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    12-13-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Help with a VBA macro ending with a LOOP

    The file does not have the same data as in all the rows until the empty one. E.g Column BS, appears to have a list of Names. In fact its pointing to a list of names. But all the names are different. After running your button, the names on the bank file appear to be the same.

    Regards

    Shil

  15. #15
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,985

    Re: Help with a VBA macro ending with a LOOP

    Try this:
    Sub dave()
    
        Dim wsBank           As Worksheet
        Dim wsData           As Worksheet
        Dim lngLastRow       As Long
        Dim lngOutRow        As Long
        Dim lngRow           As Long
    
        Set wsData = ActiveSheet
        Set wsBank = Sheets("Bank File")
        With wsBank
    
            lngOutRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
        End With
        Application.ScreenUpdating = False
    
        ' get last row in col AV
        lngLastRow = Cells(Rows.Count, "AV").End(xlUp).Row
    
        For lngRow = ActiveCell.Row To lngLastRow
            wsBank.Cells(lngOutRow, 1).Resize(, 58).Value = Cells(lngRow, "AV").Resize(, 58).Value
            lngOutRow = lngOutRow + 1
        Next lngRow
        Application.ScreenUpdating = True
    End Sub
    Everyone who confuses correlation and causation ends up dead.

  16. #16
    Registered User
    Join Date
    12-13-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Help with a VBA macro ending with a LOOP

    Quote Originally Posted by romperstomper View Post
    Try this:
    Sub dave()
    
        Dim wsBank           As Worksheet
        Dim wsData           As Worksheet
        Dim lngLastRow       As Long
        Dim lngOutRow        As Long
        Dim lngRow           As Long
    
        Set wsData = ActiveSheet
        Set wsBank = Sheets("Bank File")
        With wsBank
    
            lngOutRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
        End With
        Application.ScreenUpdating = False
    
        ' get last row in col AV
        lngLastRow = Cells(Rows.Count, "AV").End(xlUp).Row
    
        For lngRow = ActiveCell.Row To lngLastRow
            wsBank.Cells(lngOutRow, 1).Resize(, 58).Value = Cells(lngRow, "AV").Resize(, 58).Value
            lngOutRow = lngOutRow + 1
        Next lngRow
        Application.ScreenUpdating = True
    End Sub

    Thanks romperstomper. The code worked well, however i need the macro to do copy and paste row 2 (Which is part of a header record, and it gets there by typing in Ctrl+N) inbetween each line that has been copied accross.

    Any idea how to do this?

    Thanks

    Shil

  17. #17
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Help with a VBA macro ending with a LOOP

    Thanks Romperstomper, I completely missed that.

    @5h1l , sorry about that, my code was not doing what I said it would.

    It should have been this on this line.
             ws.Cells(lr, 1).Resize(, 58).Value = x.Resize(, 58).Value

  18. #18
    Registered User
    Join Date
    12-13-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Help with a VBA macro ending with a LOOP

    Hi Guys, im wondering if i havent explained myself enough:

    Here is the code:

    ActiveCell.Offset(0, 47).Range("A1:BE1").Select
        Selection.Copy
        Sheets("Bank File").Select
        Range("A1").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1, 0).Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        ActiveCell.Offset(-1, 0).Range("A1:D1").Select
        Application.CutCopyMode = False
        Selection.Copy
        ActiveCell.Offset(2, 0).Range("A1").Select
        ActiveSheet.Paste
        Sheets("Payroll Calculatios").Select
        ActiveCell.Offset(1, -47).Range("A1").Select
    This code is copying and pasting a particular row (Code in Red) and pastes to a different worksheet.

    Then it returns in the cell below the last copied cell.

    What i need to do is to somehow write a loop like the below which will keep on copying and pasting the rows to the other worksheet until it comes to an empty cell:

    Sub Test2()
    
          ' Select cell A2, *first line of data*.
          Range("A2").Select
          ' Set Do loop to stop when an empty cell is reached.
          Do Until IsEmpty(ActiveCell)
             ' Insert your code here.
             ' Step down 1 row from present location.
             ActiveCell.Offset(1, 0).Select
          Loop
       End Sub
    Does anyone know how to combine the two codes, or to edit my code so that it keeps on looping until it finds an empty cell?

    Help will be greatly appreciated.

    Thanks

    Shil
    Last edited by Cutter; 07-30-2012 at 01:20 PM. Reason: Added code tags

  19. #19
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,985

    Re: Help with a VBA macro ending with a LOOP

    Without knowing the activecell at the start, it's impossible to rewrite that code. Based on your comment about my last code, I think perhaps:
    Sub dave()
    
        Dim wsBank           As Worksheet
        Dim wsData           As Worksheet
        Dim lngLastRow       As Long
        Dim lngOutRow        As Long
        Dim lngRow           As Long
    
        Set wsData = ActiveSheet
        Set wsBank = Sheets("Bank File")
        With wsBank
    
            lngOutRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
        End With
        Application.ScreenUpdating = False
    
        ' get last row in col AV
        lngLastRow = Cells(Rows.Count, "AV").End(xlUp).Row
    
        For lngRow = ActiveCell.Row To lngLastRow
            wsBank.Cells(lngOutRow, 1).Resize(, 58).Value = Cells(2, "AV").Resize(, 58).Value
            wsBank.Cells(lngOutRow + 1, 1).Resize(, 58).Value = Cells(lngRow, "AV").Resize(, 58).Value
            lngOutRow = lngOutRow + 2
        Next lngRow
        Application.ScreenUpdating = True
    End Sub
    If not, please post before and after samples so we can see what the output should be exactly.

  20. #20
    Registered User
    Join Date
    12-13-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Help with a VBA macro ending with a LOOP

    Hi Romper Stomper, the active cell always starts in cell A5, then when it returns its in A6. Then i would execute the macro again so that it returns to A7. etc etc. Until it gets to the first blank cell, at which point i would move on to the next step.

    Does this help?

    Thanks

    Shil
    Last edited by Cutter; 07-30-2012 at 01:21 PM. Reason: Removed whole post quote

  21. #21
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,985

    Re: Help with a VBA macro ending with a LOOP

    Please comment on the last code.

  22. #22
    Registered User
    Join Date
    12-13-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Help with a VBA macro ending with a LOOP

    Thanks, this solved it!

    Regards

    Shil

+ 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