Check box if cell value is X - vba code not working... (newbie)

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi everyone, this is my first post here and I am a relative newbie with Excel VBA. I am primarily a php programmer, but I have been asked by my company to put together and develop a rather complex spreadsheet. I have the feeling that I will be asking quite a few searching questions on this forum, so please bear with me....

Firstly, I'm trying to get a checkbow to tick/untick depending on a value in another cell on the same (active) worksheet. My checkbox is 'Check Box 1' and I inserted it as a 'Form Control' not 'ActiveX' item:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    ActiveSheet.CheckBoxes("Check Box 1").Value = IIf(Range("AU13").Value = "SPT", True, False)
End Sub

but nothing happens. Am I missing something?

Thanks for your time in advance

Regards

Rob
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You need to set your target at cell au13, otherwise the code will not be called. Here is what I came up with, let me know if it works:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Me.Range("AU13")) Is Nothing Then
    If ActiveSheet.Range("AU13").Value = "SPT" Then
        ActiveSheet.Shapes("Check Box 1").ControlFormat.Value = xlOn
    Else
        ActiveSheet.Shapes("Check Box 1").ControlFormat.Value = xlOff
    End If
End If

End Sub
 
Upvote 0
Thanks seguin. As your email came in, I realised my mistake.... my code was correct, but I had MISSPELLED the name on the checkbox.... DOH! (not a great start for a first post on the forum...)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    ActiveSheet.CheckBoxes("CheckBox1").Value = IIf(Range("AS13").Value = "SPT", True, False)
End Sub
- this works fine now....

I wonder if you know how I could put an OR statement in here eg:

if Value = "STP" Or "CPT", True, False (if you get what I mean)
 
Upvote 0
I wonder if you know how I could put an OR statement in here eg:

if Value = "STP" Or "CPT", True, False (if you get what I mean)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)    
   ActiveSheet.CheckBoxes("Check Box 1").Value = IIf(Range("AS13").Value = "SPT" Or Range("AS13").Value = "CPT", True, False)
End Sub
 
Upvote 0
Ah, brilliant! Thanks mate. I've got to get my head around the VBA syntax.... it's a bit of a learning curve!
 
Upvote 0

Forum statistics

Threads
1,216,742
Messages
6,132,453
Members
449,729
Latest member
davelevnt

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