Reuse a code

santnok

Board Regular
Joined
Jan 10, 2014
Messages
97
Hi

I have this code for do a for each OptionButton in a frame

Code:
Dim ctrlOptionButton As Control
    For Each ctrlOptionButton In frameVarselTekniskeAlarmer.Controls
    If (TypeName(ctrlOptionButton) = "OptionButton") Then
    ctrlOptionButton.Value = False
    End If
Next

frameVarselTekniskeAlarmer = name of my frame.

I have lots of frames on my userforms and every frame have OptionButton, CheckBox, ComboBox.
So insted of make many of the same code as above, I hoped to maybe Reuse the same code in a function maybe?
Make one code that works for OptionButton, one for CheckBox, one for ComboBox and reuse this code on every userform that I need to use this code.

I'am not so good at vba excel and the code above I have find it with a google search :-)
Hope someone understand what I mean here :-)
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
put this code into a module. call it from every form using:

DisableCtls me

Code:
sub DisableCtls(pFrm as form)
Dim ctrlOptionButton As Control

    For Each ctrlOptionButton In pFrm.frameVarselTekniskeAlarmer.Controls
       If (TypeName(ctrlOptionButton) = "OptionButton") Then   ctrlOptionButton.Value = False
   Next
end sub
 
Last edited:
Upvote 0
Try these routines.

Code:
Sub ClearOptionButtonsInFrame(aFrame As MSForms.Frame)
    Dim oneControl As MSForms.Control
    For Each oneControl In aFrame.Controls
        If TypeName(oneControl) = "OptionButton" Then oneControl.Value = False
    Next oneControl
End Sub

Sub ClearCheckBoxesInFrame(aFrame As MSForms.Frame)
    Dim oneControl As MSForms.Control
    For Each oneControl In aFrame.Controls
        If TypeName(oneControl) = "CheckBox" Then oneControl.Value = False
    Next oneControl
End Sub

Sub ClearComboBoxesInFrame(aFrame As MSForms.Frame)
    Dim oneControl As MSForms.Control
    For Each oneControl In aFrame.Controls
        If TypeName(oneControl) = "ComboBox" Then oneControl.ListIndex = -1
    Next oneControl
End Sub

You would call them with code like

Code:
Call ClearOptionButtonsInFrame(frameVarselTekniskeAlarmer)

If you have frames within frames, the coding would be a bit more complicated if you wanted to address only the controls in the outer frame and leave the inner frame alone.
 
Last edited:
Upvote 0
Thanks for answers

mikerickson your code works perfect, thanks :-)
if I need to fix somthing within another frame I will ask you :-)

Thanks again for help :-)
 
Upvote 0
Hi again

Is it possible to do the same with this code

Code:
For Each ctrlframeEUSinfo In frameEUSinfo.Controls
        ctrlframeEUSinfo.Enabled = False
    Next ctrlframeEUSinfo

Here I have maked one for frame to be on and one that turns off the frame Enabled on/off
My code looks like this.

Code:
Public Sub F_frameMottattAlarmAV()

Dim ctrlframe1 As Control
    For Each ctrlframe1 In frmAlarmstasjon.frameMottattAlarm.Controls
        ctrlframe1.Enabled = True
    Next ctrlframe1

End Sub

Public Sub F_frameMottattSabotasjePAA()

Dim ctrlframe1 As Control
    For Each ctrlframe1 In frmAlarmstasjon.frameMottattSabotasje.Controls
        ctrlframe1.Enabled = True
    Next ctrlframe1
End Sub

Can I also here make only one for Enable = False and one for Enable = True and just use a Call function for this??
 
Last edited:
Upvote 0
Modify the subs like this
Code:
Sub SetOptionButtonsInFrameToValue(aFrame As MSForms.Frame, allValue as Boolean)
    Dim oneControl As MSForms.Control
    For Each oneControl In aFrame.Controls
        If TypeName(oneControl) = "OptionButton" Then oneControl.Value = allValue
    Next oneControl
End Sub
And call it with
Code:
Call SetOptionButtonsInFrameTo(frmAlarmstasjon.frameMottattAlarm, True)

For the ComboButton routine allValue should be data type Long.
 
Upvote 0
Hi
Thank you for the code, but I think this code does something else I hope it will do.
What I want the code to do is only make the Frame Enabled or Disabled (Enabled = True or False).


Thanks for all help
 
Last edited:
Upvote 0
Hi, I will try again, but I have done it like this.
When you open a userform I have done it so all my frames are not .Enebled. so I hope not I must do everything over :-)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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