Multiple Toggle Buttons in Excel

faberem20

New Member
Joined
Jun 26, 2013
Messages
38
Hello,

I would like to have multiple toggle buttons in excel that are independent of each other. I created two toggle buttons with the following code and get a Run-time error '1004:
Method 'OLEOBjects' of object'_Worksheet' failed. How can this be fixed?

Private Sub ToggleButton1_Click()
Range("D42").Value = IIf(Me.OLEObjects(1).Object.Caption = "Off", 0.01, 0.03)
Me.OLEObjects(1).Object.Caption = IIf(Range("D42").Value = 0.01, "On", "Off")
End Sub


Private Sub ToggleButton2_Click()
Range("D43").Value = IIf(Me.OLEObjects(1).Object.Caption = "Off", 0.01, 0.03)
Me.OLEObjects(1).Object.Caption = IIf(Range("D43").Value = 0.01, "On", "Off")
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
This worked for me.
NB:- Me.OLEObject(1) & Me.OLEObject(2)

Code:
Private Sub ToggleButton1_Click()
  'alternative code to refer to ToggleButton:-
  'Range("D4").Value = IIf(Me.OLEObjects("ToggleButton1").Object.Caption = "Off", 0.01, 0.03)
  
  Range("D4").Value = IIf(Me.OLEObjects(1).Object.Caption = "Off", 0.01, 0.03)
  Me.OLEObjects(1).Object.Caption = IIf(Range("D4").Value = 0.01, "On", "Off")
 End Sub
Private Sub ToggleButton2_Click()
 Range("D6").Value = IIf(Me.OLEObjects(2).Object.Caption = "Off", 0.01, 0.03)
 Me.OLEObjects(2).Object.Caption = IIf(Range("D6").Value = 0.01, "On", "Off")
 End Sub
 
Upvote 0
This worked for me.
NB:- Me.OLEObject(1) & Me.OLEObject(2)

Code:
Private Sub ToggleButton1_Click()
  'alternative code to refer to ToggleButton:-
  'Range("D4").Value = IIf(Me.OLEObjects("ToggleButton1").Object.Caption = "Off", 0.01, 0.03)
  
  Range("D4").Value = IIf(Me.OLEObjects(1).Object.Caption = "Off", 0.01, 0.03)
  Me.OLEObjects(1).Object.Caption = IIf(Range("D4").Value = 0.01, "On", "Off")
 End Sub
Private Sub ToggleButton2_Click()
 Range("D6").Value = IIf(Me.OLEObjects(2).Object.Caption = "Off", 0.01, 0.03)
 Me.OLEObjects(2).Object.Caption = IIf(Range("D6").Value = 0.01, "On", "Off")
 End Sub

Do you know if there is code that uses a toggle button to customize a cell to a percentage of my choosing and have the off at 3%?

Thanks,
Erik
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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