Check to see if selected cell is in a certain row

Gizmo2024

New Member
Joined
Dec 28, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I'm an absolute green newby at VBA and I can't seem to get this right. I want to check to see if the selected cell is on a certain row. If so, then I want it to perform a certain routine, and if it's not, then it is to perform a dlfferent routine. For example, these are the rows I want to check: 8, 9, 10, 18, 19, 23, 24, 25, 33, 34, 38, 39, 40, 48, 49.

Sub CheckRow()
Dim rw As Range
For Each rw In Sheets("Sheet1").Range(8, 9, 10, 18, 19, 23, 24, 25, 33, 34, 38, 39, 40, 48, 49).Row
If ActiveCell.Row = rw Then
MsgBox "Yes"
Else
MsgBox "No way"
End If

End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You can try so.
Code:
Sub CheckRow()
Dim i As Long, rwArr, cellRw
rwArr = Array(8, 9, 10, 18, 19, 23, 24, 25, 33, 34, 38, 39, 40, 48, 49)
i = ActiveCell.Row
cellRw = Application.Match(i, rwArr, 0)
    If IsNumeric(cellRw) Then
        MsgBox "Yes"
            Else
        MsgBox "No Sir"
    End If
End Sub
 
Upvote 0
You can try so.
Code:
Sub CheckRow()
Dim i As Long, rwArr, cellRw
rwArr = Array(8, 9, 10, 18, 19, 23, 24, 25, 33, 34, 38, 39, 40, 48, 49)
i = ActiveCell.Row
cellRw = Application.Match(i, rwArr, 0)
    If IsNumeric(cellRw) Then
        MsgBox "Yes"
            Else
        MsgBox "No Sir"
    End If
End Sub
That worked like a charm. Thank you so much!!
 
Upvote 0
Thanks for the update and have a Happy, Healthy and Prosperous New Year.

BTW, in your future answers, don't quote whole posts. Just clutter for people that copy threads to help others.
 
Upvote 0

Forum statistics

Threads
1,226,488
Messages
6,191,313
Members
453,653
Latest member
mvillasana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top