Referencing a big number of ToggleButtons

Stavarek

New Member
Joined
Feb 18, 2020
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I have a table which has a ToggleButton next to each line.
I would like to write a script that does something for each line of the table, but only if the ToggleButton on that line is pressed.

Is there a way to define the if condition in an elegant way?

I would imagine something like:

For i = 1 To 100
if ("ToggleButton" & (i + 29)).Value = True Then
'DO SOMETHING
End If
Next i

Or:

For i = 1 To 100
if CallByName(("ToggleButton" & (i + 29)), Value, VbGet) = True Then
'DO SOMETHING
End If
Next i

Unfortunately, none of these above work and I have searched for hours trying to find a solution without having to write down 100 different button names into a collection or something...
Please help me.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Perhaps like this:

Code:
For i = 1 To 100
if Activesheet.OleObjects("ToggleButton" & (i + 29)).Object.Value = True Then
'DO SOMETHING
End If
Next i
 
Upvote 0
Perhaps like this:

Code:
For i = 1 To 100
if Activesheet.OleObjects("ToggleButton" & (i + 29)).Object.Value = True Then
'DO SOMETHING
End If
Next i

Thank you, this works perfect!
 
Upvote 0
Hi there. If you associate each toggle button with the cell underneath it, you can test the value of that cell directly. e.g. if your toggle buttons are associated with rows in column A, then this loop would do it:
VBA Code:
For i = 1 To 100
  if Range("A" & (i + 29)).value= True Then
       'DO SOMETHING
  End If
Next i
 
Upvote 0

Forum statistics

Threads
1,214,868
Messages
6,122,005
Members
449,059
Latest member
mtsheetz

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