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

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
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,225,202
Messages
6,183,537
Members
453,168
Latest member
Luggsy

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