Relative Cell Reference for a Macro

Confused Whitch

New Member
Joined
Feb 4, 2015
Messages
1
Sorry I am relatively new to making Macros, I have a basic macro that hides a row when a checkbox is selected. However I don't want to copy and paste the same macro for every line I have the checkbox in.

My (very basic code) is:

ub CheckBox()


If Range("M4").Value = True Then
Call Macro1
Else: Range("M4").Value = False
Call Macro2

End If


End Sub


Sub Macro1()
'
' Macro1 Macro
' Hides a Row
'
' Keyboard Shortcut: Ctrl+h
'
ActiveCell.EntireRow.Hidden = True

End Sub


Sub Macro2()
'
' Macro2 Macro
' Unhides a Row
'
' Keyboard Shortcut:
'
ActiveCell.EntireRow.Hidden = False

End Sub


I need M4 to change to M5 and so on as insert lines etc.... Can anyone point out the obvious mistake to me please.
 
try

Code:
Sub ConfusedWhitch()


Dim LR As Long, i As Long
LR = Range("M" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For i = LR To 1 Step -1
    If Range("M" & i).Value = True Then Call Macro1
    ElseIf Range("M" & i).Value = False Then Call Macro2
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0

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