ws1.Cells(1, i).Copy ws2.Range("a65536").End(xlUp).Offset(1, 0)
I am looping through the sheet so i is a part of loop. The number of rows are more than 65536 , then how can I adjust this. Thanks in advance.
ws1.Cells(1, i).Copy ws2.Range("a65536").End(xlUp).Offset(1, 0)
I am looping through the sheet so i is a part of loop. The number of rows are more than 65536 , then how can I adjust this. Thanks in advance.
You should use a count of rows rather than a hard coded value.
With ws2
ws1.Cells(1, i).Copy .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
End With
Simplest approach is wks.usedrange.rows.count
. Reasonably sure the bugs related to usedrange updating when saving the workbook were fixed in post-excel...
All concise solutions have cases where they fail. The accepted answer will produce the wrong result in the circumstance where the column is completely blank.
Here is a different way that works as long as there are no blanks above or in-between the data:
ws1.Cells(, i).Copy ws2.[INDEX(A:A,COUNTA(A:A)+1)]
Remember that you can always get the last used range on a excel worksheet as follow:
Dim lastRange as Range
Dim lastColumn as Long
Dim lastRow as Long
Set lastRange = Worksheets(?).UsedRange.SpecialCells(xlCellTypeLastCell)
lastColumn = lastRange.Column 'return the last column index
lastRow = lastRange.Row 'return the last row index
So to answer your question, you can use my previous example like this:
ws1.Cells(1, i).Copy ws2.Range("A" & lastRow).Offset(1, 0) 'or
ws1.Cells(1, i).Copy ws2.Cells(lastRow, 1).Offset(1, 0)
Both statements will do the job for you. Just be conscious that you have to redefine the lastRange Object and the lastRow variable within the loop, otherwise the lastRange variable will hold the value of its initialization.
Good Luck!
Andrés Alejandro García Hurtado.