Sorry, but I do not agree with Michael's answer.
End (xlDown) - VBA equivalent by pressing Ctrl
+ Down
.
Try Ctrl
+ Down
with
- empty column
- column with a value in row 1 but not another
- values ββin lines 1 and 2
- the values ββin lines 1, 2, 3, 7, 8, 9, 13, 14 and 15
This will give you an idea of ββall the different lines, Ctrl
+ Down
may keep you busy.
Set newRange = ws.Range("A1").End(xlDown).End(xlDown).End(xlUp).Offset(1, 0)
will not necessarily lead you to the last line used plus 1.
I am surprised that Set newRange = ws.Range("A1").End(xlDown).Offset(1, 0)
worked with an empty column. Range("A1").End(xlDown)
will bring you to the bottom line of the sheet, then .Offset(1, 0)
will try to get you out of the sheet.
Consider:
Dim RowLast As Long RowLast = ws.Cells(Rows.Count, "A").End(xlUp).Row
- If column A is empty, RowLast will be set to 1.
- If the value A1 matters, but no other cells matter, RowLast will be set to 1.
- If the number of cells in column A matters, RowLast will be set to the bottom row with the value.
- If you have a value in the last line, it will be ignored.
- If you have a value in the last two lines, RowLast will be set to Rows.Count - 1.
I assume that you have no values ββin the lines of the borm. If you don't care if row 1 is left empty with an empty column, then:
RowLast = ws.Cells(Rows.Count, "A").End(xlUp).Row Set NewRange = ws.Cells(RowLast + 1, "A")
should give the desired result regardless of the current contents of the ws sheet.
If you don't care that line 1 is left blank, the experiment with Ctrl
+ Down
and Ctrl
+ Up
will give you an idea of ββthe effect of various combinations of values.
source share