Hi guys,
I have the following VBA script that doesn't quite work for some reason.
Basically I want the script to place the cursor at the 1st free cell of either of my 3 columns depending on which columns have more rows of data. Is there any obvious error in my code? See what happens is if lr1 = lr 3 it sometimes randomly (not all the time) places the cursor at the end of column D instead of A and also if lr3 < lr2 it sometimes places the cursor at end of column A when it should be column AP, it does my head in! Each time I run the macro I seem to get a different result
Sub Update_Cursor()
Dim lr1 As Long
Dim lr2 As Long
Dim lr3 As Long
Dim ws As Worksheet
lr1 = Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Cells(Rows.Count, "D").End(xlUp).Row
lr3 = Cells(Rows.Count, "AP").End(xlUp).Row
Sheets("a").Select
If lr1 = lr3 Then
Range("A2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ElseIf lr3 < lr2 Then
Range("AP2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Else
Range("D2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
End If
End Sub
I have the following VBA script that doesn't quite work for some reason.
Basically I want the script to place the cursor at the 1st free cell of either of my 3 columns depending on which columns have more rows of data. Is there any obvious error in my code? See what happens is if lr1 = lr 3 it sometimes randomly (not all the time) places the cursor at the end of column D instead of A and also if lr3 < lr2 it sometimes places the cursor at end of column A when it should be column AP, it does my head in! Each time I run the macro I seem to get a different result
Sub Update_Cursor()
Dim lr1 As Long
Dim lr2 As Long
Dim lr3 As Long
Dim ws As Worksheet
lr1 = Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Cells(Rows.Count, "D").End(xlUp).Row
lr3 = Cells(Rows.Count, "AP").End(xlUp).Row
Sheets("a").Select
If lr1 = lr3 Then
Range("A2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ElseIf lr3 < lr2 Then
Range("AP2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Else
Range("D2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
End If
End Sub