Sub worksheet_change(ByVal target As Range)

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,516
Office Version
  1. 2016
Platform
  1. Windows
hello,

i am using this code to call macros based on cell value.

Code:
Sub worksheet_change(ByVal target As Range)
Set target = Range("A1")
If target.Value = "1" Then
 Call onepo
End If
If target.Value = "2" Then
Call twopo
End If
If target.Value = "3" Then
Call threepo
End If
If target.Value = "4" Then
Call fourpo
End If
If target.Value = "5" Then
Call fivepo
End If
If target.Value = "6" Then
Call sixpo
End If


End Sub

Only problem i am having is whenever any other work i do on the sheet the macro triggers.
Whereas i only want the macros to triger when the value of cell A1 changes. Like when i enter anything
in cell (lets say) b2 and as soon as i press enter the macro triggers :(

Secondly cell A1 has a data validation list. i want to call the macro (sixpo) when the cell is blank.
i dont know how to enter it in the above code.

i would be grateful if anyone can help.

Thanks

Humayun
 
Last edited:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You do not set "Target" in these procedures, "Target" is automatically the cell that was changed that is calling the procedure.
So in your case here, we just need to see if the Target cell (the cell being changed), is cell A1.

Here is how we can write the code to do all that you want:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Count = 1 And Target.Address = Range("A1").Address Then
        Select Case Target.Value
            Case "1"
                Call onepo
            Case "2"
                Call twopo
            Case "3"
                Call threepo
            Case "4"
                Call fourpo
            Case "5"
                Call fivepo
            Case "6"
                Call sixpo
            Case ""
                Call sixpo
        End Select
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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