Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select next non empty cell in A column – Excel VBA

Trying to have a button to select next non empty/blank cell in column A, relative to current row. The code below works, but only if active cell is in column A.
Need it to work on column A, even when active cell is in another column.

Private Sub CommandButton3_Click() 'Next step button - selects next step in A column
Dim n As Long
n = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
If ActiveCell.Row = n Then MsgBox "Last one, no more instructions.": Exit Sub

If ActiveCell.Offset(1, 0) = "" Then
    ActiveCell.End(xlDown).Select
Else
    ActiveCell.Offset(1, 0).Select
End If

End Sub

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The problem is your activecell is in the wrong column. You need to address that:

Private Sub CommandButton3_Click() 'Next step button - selects next step in A column
Dim n As Long, fixed_range As Range
Set fixed_range = ActiveSheet.Cells(ActiveCell.Row, 1)
n = Cells(Rows.Count, fixed_range.Column).End(xlUp).Row
If fixed_range.Row = n Then MsgBox "Last one, no more instructions.": Exit Sub

If fixed_range.Offset(1, 0) = "" Then
    fixed_range.End(xlDown).Select
Else
    fixed_range.Offset(1, 0).Select
End If
End Sub

This uses a "fixed_range" to make sure we’re working on column 1.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading