Incrementing checkbox numbers with VBA.

medic5678

Board Regular
Joined
Nov 13, 2018
Messages
67
I have 10 checkboxes that I need to set value to FALSE

They are named NACB101 thru NACB110.

for x = 1 to 10
NACB101.value = FALSE (not sure how to concatenate the name of the textbox here so I can increment with a counter).

next x

Thanks for your help here.
 
Could I pass this a frame name, so I could use one piece of code to clean all the check boxes in any frame (or for that matter, manipulate any group of controls' attribultes)? And would you put this code in the forms code, or in a module? I'm honestly very puzzled by when you should put code into a form vs a module.
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Could I pass this a frame name, so I could use one piece of code to clean all the check boxes in any frame (or for that matter, manipulate any group of controls' attribultes)? And would you put this code in the forms code, or in a module? I'm honestly very puzzled by when you should put code into a form vs a module.
See Message #9
 
Upvote 0
I'm confused what your question is.

You put the script normally in a command button on the Userform
When you click the button the script runs. I provided you a script earlier that you said worked.
But you can also put the script in any control you want. You can assign a script to nearly any control you have.
Maybe if your wanting one script to do numerous things you should tell us what your ultimate goal is.
 
Upvote 0
I'm confused what your question is.

You put the script normally in a command button on the Userform
When you click the button the script runs. I provided you a script earlier that you said worked.
But you can also put the script in any control you want. You can assign a script to nearly any control you have.
Maybe if your wanting one script to do numerous things you should tell us what your ultimate goal is.

Well, let's say, for example, that I wanted to use the same code with a number of command buttons, but within different frames on the same form... and only to have the code act on the objects within that frame. I could make a subroutine that was called to clear the checkboxes in any frame. So when you clicked a command button, it would call the subroutine and pass it the name of the frame to act upon. So this subroutine itself could be in a module or it could be in a frame's code. Trying to get a sense of where to put that and why.
 
Upvote 0
If you leave your Frames with their default names (Frame1, Frame2, etc.), then here is a subroutine that you can pass the frame number into and it will clear all the checkboxes within that Frame. The subroutine name is ClearFrameCheckBoxes and you would call it from you event code (say, the event code for a CommandButton) like this to, for this example, clear Frame2...

ClearFrameCheckBoxes 2

Here is the subroutine code (you can place it in the UserForm's code module...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub ClearFrameCheckBoxes(FrameNum As Long)
  Dim Cntrl As Control
  For Each Cntrl In Controls("Frame" & FrameNum).Controls
    If TypeOf Cntrl Is MSForms.CheckBox Then Cntrl.Value = False
  Next
End Sub[/TD]
[/TR]
</tbody>[/TABLE]


Ok, got it working, passing the frame name as a string, which allows me to run it from any control in any frame. Still trying to wrap my head around these control objects :).
 
Upvote 0
I guess Rick gave you what you wanted. And you do not need my help any more. Take care.
Ok, got it working, passing the frame name as a string, which allows me to run it from any control in any frame. Still trying to wrap my head around these control objects :).
 
Upvote 0
I guess Rick gave you what you wanted. And you do not need my help any more. Take care.

Ha. Of course I need your help! You are a master in my eyes. Your solutions are awesome and full of learning opportunities. As far as my actual use goes, I'm just trying to get this working and get an understanding of VBA, then I'll try to compress the code and make the whole thing more efficient. Very serious about learning good programming practices with VBA. I'm sure I'll also use your code as well in another part of this.
 
Upvote 0
Glad your learning more here each day.
It's always best to try and understand the code if you can.
It's always best to:
Teach a Man to Fish then to Fish for him.

If you wanted to set all your controls back to the condition they were in when you opened the userform you could use this script.
Change userform name as needed.

Code:
Private Sub CommandButton7_Click()
'Modified  11/19/2018  10:14:37 AM  EST
Unload Me
UserForm1.Show
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,237
Messages
6,123,807
Members
449,127
Latest member
Cyko

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