1

I want to make a sub that inserts a picture if checkbox is checked and deletes it if it's unchecked, this is what i've got so far, and the first part (insert when checked) works fine. Any ideas, please?

Dim chbx
Set chbx = ActiveSheet.CheckBoxes.Add(240, 15, 144, 15.75)
chbx.Characters.Text = "DisplacementPicturesIns"
chbx.OnAction = "DisplacementPicturesIns"
If chbx.Value = True Then
    chbx.OnAction = True
Elseif chbx.Value = False Then
....
End If
1
  • 1
    Your code doesn't make a sense since OnAction must hold the name of procedure. Commented May 21, 2018 at 5:20

2 Answers 2

1

I dunno if you still need this but here is a solution that i made to select/unselect my checkboxes elements. you can change between the if statement to do what ever you want :)

Sub SelectAll()
   Dim chkbx As CheckBox
   Dim cbv As Long

   cbv = Sheets("Feuil1").CheckBoxes(Application.Caller).Value

   If cbv = 1 Then
      For Each chkbx In Sheets("Feuil1").CheckBoxes
         chkbx.Value = xlOn
      Next
   Else
      For Each chkbx In Sheets("Feuil1").CheckBoxes
         chkbx.Value = xlOff
      Next
   End If
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Yeh, thanx, the more solutions the better!
0

Like it was pointed out in the notes, your code doesnt make much sense. the OnAction holds the name of the method to be called once the checkbox is ticked/unticked. Also, your condition is only being tested once, after the creation of the Checkbox control and thats it. if you want your condition to be tested every time, it must be placed inside the sub that is called on the ticking/unticking event.

My suggestion: give the checkbox a name, and then refer to it inside the sub and check it's value:

Public Sub Example()
    With ActiveSheet.CheckBoxes.Add(240, 15, 144, 15.75)
        .Characters.Text = "DisplacementPicturesIns"
        .Name = "myCheckBox"
        .OnAction = "myCheckBox_Click"
    End With
End Sub
Sub myCheckBox_Click()
    Dim chbx
    Set chbx = ActiveSheet.CheckBoxes("myCheckBox")
    If chbx.Value = 1 Then
        DisplacementPicturesIns
    Else
        'do other stuff 
    End If
End Sub

1 Comment

Yeh, thanx. that makes sence!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.