Results 1 to 5 of 5

Thread: Moving data

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    VBAX Contributor Richie(UK)'s Avatar
    Joined
    May 2004
    Location
    UK
    Posts
    188
    Location
    Hi b,

    Welcome to the board

    One approach would be to use a small function to establish the next row to use each time through the loop. Something like this:

    Sub Test()
        Dim lRow As Long
        lRow = NextRow("Sheet1")
        Worksheets("Sheet1").Cells(lRow, "A").Value = "Test"
    End Sub
    
    Function NextRow(strSheetName As String) As Long
        Dim lLast As Long
        Const lStart As Long = 8  'the default start row
        Const lOffset As Long = 6 'the number of rows to offset by
    lLast = Worksheets(strSheetName).Cells(Rows.Count, "A").End(xlUp).Row
        If lLast < 8 Then
        NextRow = lStart
        Else
        NextRow = lLast + lOffset
        End If
    End Function
    Try it in a blank workbook and you'll soon get the idea of how it works.

    A couple of bits of advice, if you are interested.

    1. Get into the habit of declaring row variables as Longs rather than Integers. The latter may not always be sufficient and its one less thing to worry about when error-checking if you always use Longs.

    2. Avoid using Select and Activate in your code where possible - they are rarely necessary and just make your code slower.

    HTH
    Last edited by Aussiebear; 04-29-2023 at 08:21 PM. Reason: Adjusted the code tags

Posting Permissions

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