Forum Discussion
using VBA to select first empty row.
Hi Katrina,
I'll point you in this direction and give a few tips
Check out this bit of code
Sub FindLastCell()
Dim LastCell As Range
Dim LastCellColRef As Long
LastCellColRef = 2 'column number to look in when finding last cell
Set LastCell = Sheets("UNNEEDED").Cells(Rows.Count, LastCellColRef).End(xlUp).Offset(1, 0)
MsgBox LastCell.Address
Set LastCell = Nothing
End Sub
The first change I would make to this code is to replace the Sheets("UNNEEDED") part with the underlying sheet number e.g Sheet2.cells or Sheet3.cells You can see that sheet number next to the name in the VBA editor window. The main reason is to prevent your code breaking if the sheet is renamed.
The Dim part is setting the variables to use in your code. This is a very important concept to learn about when writing VBA.
I've made LastCellColRef as a variable as this makes it clear that it is column 2 that is being searched in to find the last entry
Once you've SET your LastCell you can use it as a reference in your code
e.g.
Selection.Copy
LastCell.PasteSpecial xlPasteValues
Tips
Once you've set a variable, then at the end of your routine you should set it = nothing to clear it from memory.
Try to avoid cell referencing when writing VBA e.g. Range("$A$2:$DM$8442), use named ranges instead.