Creating a macro for the extensive repetitive code

inactiveUser214710

Board Regular
Joined
Apr 27, 2012
Messages
171
Hi everyone
Please Help me
In my vba project, I have three togg buttons (Tog1, Tog2 and Tog3) whose purpose is to open and close specifically (textboxes and comboboxes).

Thus: Tog1 opens and closes textbox1 and combobox1 and blocks Tog2 and Tog3
Tog2 opens and closes textbox2 and combobox2 and blocks Tog1 and Tog3
Tog3 opens and closes textbox3 and combobox3 and blocks Tog1 and Tog2

The code I use is described below, which becomes very extensive depending on the amount of Togs and Textbox and combobox used.
The question is how to create a macro for this purpose.
Thank you.

Code:
Private Sub Tog1_Click ()
If Tog1.Value = True Then
Tog2.Locked = True
Tog3.Locked = True
ElseIf Tog1.Value = False Then
Tog1.Locked = False
Tog2.Locked = False
End if
End Sub
Private Sub Tog2_Click ()
  ........
Private Sub Tog3_Click ()
 .........
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Untested:

Code:
Private Sub Tog1_Click()
  TogClick Tog1
End Sub

Private Sub Tog2_Click()
  TogClick Tog2
End Sub

' ...

Sub TogClick(tog As ToggleButton)
  Static col        As Collection
  Dim ctl           As Control
  Dim vCtl          As Variant

  If col Is Nothing Then
    Set col = New Collection
    For Each ctl In Me.Controls
      If TypeName(ctl) = "ToggleButton" Then col.Add ctl
    Next ctl
  End If

  For Each vCtl In col
    If Not vCtl Is tog Then vCtl.Locked = tog.Value
  Next vCtl
End Sub
 
Upvote 0
Untested:

Code:
Private Sub Tog1_Click()
  TogClick Tog1
End Sub

Private Sub Tog2_Click()
  TogClick Tog2
End Sub

' ...

Sub TogClick(tog As ToggleButton)
  Static col        As Collection
  Dim ctl           As Control
  Dim vCtl          As Variant

  If col Is Nothing Then
    Set col = New Collection
    For Each ctl In Me.Controls
      If TypeName(ctl) = "ToggleButton" Then col.Add ctl
    Next ctl
  End If

  For Each vCtl In col
    If Not vCtl Is tog Then vCtl.Locked = tog.Value
  Next vCtl
End Sub

Thank you very much
Perfect, Impeccable
Solved
 
Upvote 0
You're welcome.
hi SHG
I'm sorry for being incomplete in my first macro problem
about togglebutton.
It was missing the detail that when tog1,2,3 opened the button, the name of the button should be written on the worksheet.
please fill in this macro detail in your intervention.
I'm waiting and thank you again
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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