Repetion of code

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,644
Office Version
  1. 365
Platform
  1. Windows
I was wondering if there was a possible solution available for the repetion of code that relates to multiple objects (combobox's, buttons, check box's) on a user form?

I have a form that contains a multipage tab (7 pages/tabs) and on each page there are objects that are duplicated on the other pages but with slightly different names to distinguish which pages they are on e.g. cboxCheckBox1Page1, cboxCheckBox2Page1, cboxCheckBox1Page2, cboxCheckBox2Page2, etc, etc.

Example of code for Checkbox's
Code:
Private Sub cboxCopyPriorTueWk1_Click()

If cboxCopyPriorTueWk1 = True Then
    boolPopulateForm = True
    cbCoreStartTueWk1 = cbCoreStartMonWk1
    cbCoreFinishTueWk1 = cbCoreFinishMonWk1
    boolPopulateForm = False
    
    boolTueWk1Correct = True
    
    cboxCopyPriorWedWk1.Enabled = True
    cbCoreStartTueAll.Enabled = False
    cbCoreFinishTueAll.Enabled = False
    
    btnTueAllShift1.Enabled = False
    btnTueAllShift2.Enabled = False
    btnTueAllShift3.Enabled = False
    btnTueAllShift4.Enabled = False
    
    Else
End If

End Sub
Code:
Private Sub cboxCopyPriorWedWk1_Click()

If cboxCopyPriorWedWk1 = True Then
    boolPopulateForm = True
    cbCoreStartWedWk1 = cbCoreStartTueWk1
    cbCoreFinishWedWk1 = cbCoreFinishTueWk1
    boolPopulateForm = False
    
    boolWedWk1Correct = True
    
    cboxCopyPriorThuWk1.Enabled = True
    cbCoreStartWedAll.Enabled = False
    cbCoreFinishWedAll.Enabled = False
    
    btnWedAllShift1.Enabled = False
    btnWedAllShift2.Enabled = False
    btnWedAllShift3.Enabled = False
    btnWedAllShift4.Enabled = False
    
    Else
End If

End Sub
Code:
Private Sub cboxCopyPriorTueWk2_Click()

If cboxCopyPriorTueWk2 = True Then
    boolPopulateForm = True
    cbCoreStartTueWk2 = cbCoreStartMonWk2
    cbCoreFinishTueWk2 = cbCoreFinishMonWk2
    boolPopulateForm = False
    
    boolTueWk2Correct = True
    
    cboxCopyPriorWedWk2.Enabled = True
    cbCoreStartTueAll.Enabled = False
    cbCoreFinishTueAll.Enabled = False
    
    btnTueAllShift1.Enabled = False
    btnTueAllShift2.Enabled = False
    btnTueAllShift3.Enabled = False
    btnTueAllShift4.Enabled = False
    
    Else
End If

End Sub
Code:
Private Sub cboxCopyPriorWedWk2_Click()

If cboxCopyPriorWedWk2 = True Then
    boolPopulateForm = True
    cbCoreStartWedWk2 = cbCoreStartTueWk2
    cbCoreFinishWedWk2 = cbCoreFinishTueWk2
    boolPopulateForm = False
    
    boolWedWk2Correct = True
    
    cboxCopyPriorThuWk2.Enabled = True
    cbCoreStartWedAll.Enabled = False
    cbCoreFinishWedAll.Enabled = False
    
    btnWedAllShift1.Enabled = False
    btnWedAllShift2.Enabled = False
    btnWedAllShift3.Enabled = False
    btnWedAllShift4.Enabled = False
    
    Else
End If

End Sub
Basically, I was hoping there was one piece of code that will cater for each of the 42 checkbox's on the 7 tabs and also the numerous combobox's and buttons that do similar things.


Thanks
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
You could pass the control to a common procedure to take care of some of it. Below is a greatly oversimplified example using "Select Case". you could use the controls name and/or type or whatever else is available.

The "Tag" property of most, if not all, controls is a string available for exclusive use by you, the user. You could assign a logical pattern of codes to each tag string at design time. It might make your code easier to read and understand if someone else needs to work with it.

Hope it helps.

Gary

Code:
Private Sub CommandButton1_Click()

Test UserForm1.CommandButton1

End Sub

Code:
Private Sub CommandButton2_Click()

Test UserForm1.CommandButton2

End Sub

Code:
Public Sub Test(oControl As Object)

'Change the clicked command button caption to the current time
oControl.Caption = Time

MsgBox oControl.Name

Select Case Left(oControl.Tag, 3)

    Case "Mon"
    
        Select Case Mid(oControl.Tag, 4, 3)
        
            Case "Jan"
                'Code for control tagged "MonJan..."
            Case "Feb"
            
            Case "Mar"
            
        End Select
        
    Case "Tue"
    
    Case "Wed"

End Select

End Sub
 
Upvote 0
Do you really need all those controls?

The only thing which changes in you code is the day being processed. So you only need one set of controls. For example:

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] UserForm_Initialize()
   [COLOR=darkblue]With[/COLOR] cboDay
      .AddItem "Mon"
      .AddItem "Tue"
      .AddItem "Wed"
      .AddItem "Thu"
      .AddItem "Fri"
      .AddItem "Sat"
      .AddItem "Sun"
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] cboDay_Change()
   [COLOR=darkblue]Select[/COLOR] [COLOR=darkblue]Case[/COLOR] cboDay.Value
      [COLOR=darkblue]Case[/COLOR] "Mon"
         Process ("Sun")
      [COLOR=darkblue]Case[/COLOR] "Tue"
         Process ("Mon")
      [COLOR=darkblue]Case[/COLOR] "Wed"
         Process ("Tue")
      [COLOR=darkblue]Case[/COLOR] "Thu"
         Process ("Wed")
      [COLOR=darkblue]Case[/COLOR] "Fri"
         Process ("Thu")
      [COLOR=darkblue]Case[/COLOR] "Sat"
         Process ("Fri")
      [COLOR=darkblue]Case[/COLOR] [COLOR=darkblue]Else[/COLOR]
         Process ("Sat")
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Select[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] Process([COLOR=darkblue]ByVal[/COLOR] myDay [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR])
 
   [COLOR=green]'your code goes here[/COLOR]
 
   MsgBox "Day being processed is: " & myDay
 
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Edit: Gary beat me to it.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,863
Members
452,948
Latest member
UsmanAli786

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