way to reduce the amount of code excel 2003

jim101

Board Regular
Joined
Mar 22, 2005
Messages
110
I am using this to make sure I am in one of these cells before my macro runs, is there and easier way to write this

Code:
 'check to see if you are in the right cells
   If _
   ActiveCell.Address = Range("A4").Address Or ActiveCell.Address = Range("G4").Address Or _
   ActiveCell.Address = Range("M4").Address Or ActiveCell.Address = Range("S4").Address Or _
   ActiveCell.Address = Range("A12").Address Or ActiveCell.Address = Range("G12").Address Or _
   ActiveCell.Address = Range("M12").Address Or ActiveCell.Address = Range("S12").Address Or _
   ActiveCell.Address = Range("A20").Address Or ActiveCell.Address = Range("G20").Address Or _
   ActiveCell.Address = Range("M20").Address Or ActiveCell.Address = Range("S20").Address Or _
   ActiveCell.Address = Range("A28").Address Or ActiveCell.Address = Range("G28").Address Or _
   ActiveCell.Address = Range("M28").Address Or ActiveCell.Address = Range("S28").Address Or _
   ActiveCell.Address = Range("A36").Address Or ActiveCell.Address = Range("G36").Address Or _
   ActiveCell.Address = Range("M36").Address Or ActiveCell.Address = Range("S36").Address Or _
   ActiveCell.Address = Range("A44").Address Or ActiveCell.Address = Range("G44").Address Or _
   ActiveCell.Address = Range("M44").Address Or ActiveCell.Address = Range("S44").Address Or _
   ActiveCell.Address = Range("A52").Address Or ActiveCell.Address = Range("G52").Address Or _
   ActiveCell.Address = Range("M52").Address Or ActiveCell.Address = Range("S52").Address Or _
   ActiveCell.Address = Range("A60").Address Or ActiveCell.Address = Range("G60").Address Or _
   ActiveCell.Address = Range("M60").Address Or ActiveCell.Address = Range("S60").Address Or _
   ActiveCell.Address = Range("A68").Address Or ActiveCell.Address = Range("G68").Address Or _
   ActiveCell.Address = Range("M68").Address Or ActiveCell.Address = Range("S68").Address Or _
   ActiveCell.Address = Range("A76").Address Or ActiveCell.Address = Range("G76").Address Or _
   ActiveCell.Address = Range("M76").Address Or ActiveCell.Address = Range("S76").Address Then

Thanks
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
So you want to check if the active cell is A4, G4, M4 or S4 or A12, G12, M12, S12 and so on moving 8 rows down each time.
Code:
Dim rng As Range
Dim boolInRange As Boolean
Dim I As Long
Set rng = Range("A4, G4, M4, S4")
    For I = 1 To 10
    
        boolInRange = boolInRange Or Not (Intersect(rng, ActiveCell) Is Nothing)
        
        Set rng = rng.Offset(8)
    Next I
    
    If boolInRange Then
        MsgBox "In range"
        ' do stuff
    End If
 
Upvote 0
One way, there's probably more elegant ways...

Code:
Sub test()
Dim MyCol As Long, MyRow As Long
MyCol = ActiveCell.Column
MyRow = ActiveCell.Row
If MyCol Mod 6 = 1 And _
    MyRow Mod 8 = 4 And _
    MyCol < 20 And _
    MyRow < 77 Then
    
    MsgBox "yes"
Else
    MsgBox "no"
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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