ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to check if a cell is part of a series of cells (not a neat ra (https://www.excelbanter.com/excel-programming/364964-re-how-check-if-cell-part-series-cells-not-neat-ra.html)

TDW

How to check if a cell is part of a series of cells (not a neat ra
 
Hi,

Basically, you want to know if you're in column 2 and row 10, 15, 20, ...
80. Your row numbers are multiples of 5 which can be verified using modulus
arithmetic.

The following code will perform that check for you:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 And _
2 <= Target.Row \ 5 And Target.Row \ 5 <= 16 And _
Target.Row Mod 5 = 0 Then
MsgBox "I'm in one of the target cells"
End If
End Sub

It would be slightly faster if you used With - End With so that Target is
not referenced multiple times.

HTH,
tdw


--
Timothy White
Contract Programmer
Ontario, Canada

<my initialshite<atsympatico<dot<countryCode



"JeKaRe" wrote:

Hi All,

I want to find out if a changed cell was one of a number of cells.
The cells i want to test for are B10,B15,B20,B25,B30 etc. upto B80.

I can use the IF statement with a lot of ORs to check this but surely there
is another, cleaner way to do this. Can anyone tell me how?

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$B$10" OR
Target.Address = "$B$15" OR
Target.Address = "$B$20" OR ....... THEN

code to execute

END IF

end sub



All times are GMT +1. The time now is 06:54 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com