Forum Discussion

djreyes's avatar
djreyes
Copper Contributor
Jun 30, 2023

VBA Macro

Hi, 

 

Please let me know if I am missing something. 

 

'Find the next available row in the destination sheet
nextRow = destinationSheet.Cells(destinationSheet.Rows.Count, 1).End(xlUp).Row + 1

 

Instead of going to the first row, its going to the second row. 

Please see attached screencapture. 

 

Thanks for the help!

5 Replies

  • djreyes 

    If the entire column is still empty, destinationSheet.Cells(destinationSheet.Rows.Count, 1).End(xlUp).Row will be 1, so if you add 1 you end up with 2.

    You could use

     

        Dim rngLastCell As Range
        Set rngLastCell = destinationSheet.Cells(destinationSheet.Rows.Count, 1).End(xlUp)
        If rngLastCell.Value = "" Then
            nextRow = 1
        Else
            nextRow = rngLastCell.Row + 1
        End If
    • djreyes's avatar
      djreyes
      Copper Contributor
      I tried and it didnt work, should I add it like this?

      ' Find the next available row in the destination sheet
      Dim rngLastCell As Range
      Set rngLastCell = destinationSheet.Cells(destinationSheet.Rows.Count, 1).End(xlUp)
      If rngLastCell.Value = "" Then
      nextRow = 1
      Else
      nextRow = rngLastCell.Row + 1
      End If
      • djreyes 

        The code that I posted should replace the code that you included in the first post. How you use nextRow is up to you.

        The code calculates nextRow correctly - I tested it. What is the problem you're having with it?

Resources