PDA

View Full Version : Solved: making code as reusable/simple as possible



gibbo1715
09-03-2005, 01:37 PM
Im spending a lot of time lately trying (As i know i should) to make my code as simple and re usable as possible and with that im mind would like to know if the following idea is possible in VBA



Private Sub CommandButton1.Click()

Dim i As Integer

For i = 1 To 5
If UserForm1.Controls("Checkbox" & i) = True Then
call CheckBoxMacro_i '<<<<< Where i = Number
End If
Next i

End Sub
Private Sub CheckBoxMacro_1()

MsgBox "Hello1"

End Sub

Private Sub CheckBoxMacro_2()

MsgBox "Hello2"

End Sub

'Etc............



Cheers

Gibbo

mdmackillop
09-03-2005, 01:50 PM
Hi Gibbo,
The following will run macros stored in a standard module

Sub CommandButton1_Click()

Dim i As Integer

For i = 1 To 5
If UserForm1.Controls("Checkbox" & i) = True Then
Application.Run "CheckBoxMacro_" & i
End If
Next i

End Sub



I'm sure there must be a way to call a macro stored on the userform, but I haven't figured it out yet!

Regards
MD

gibbo1715
09-03-2005, 02:05 PM
Thanks MD

That makes things a lot simpler

Gibbo