0

I have an UserForm in Excel VBA with different Objects like CheckBox, RadioButtons, TextEdit Box etc. Each Object has different event functions, but i want to call the some event functions in user defined sub function like UserForm_Init() function in order to re-utilize the same code written in them.

Example:

Private Sub  CheckBox1_Click() [Later changed to public also]
    If CheckBox1.Value Then
         --- Code
    End If
End Sub

Private Sub    CheckBox2_Click()
      If CheckBox2.Value Then
         --- Code
      End If
End Sub

Private Sub    RadioButton1_Click()
    If RadioButton1.Value Then
         --- Code
    End If
End Sub

' User Defined Function
Sub UserForm_Init()
     CheckBox1_Click()
     CheckBox2_Click()
     RadioButton1_Click()
End Sub

But when i am trying to compile it is showing compile error, how can i execute these object event functions within user-defined sub function.

I know I can write individual functions within Event functions and call these individual functions for user-defined sub function. But in this case, it will be too-difficult to re-change entire code. I have only option of calling these event-functions in an sub-function for execution.

How can i perform these actions, call event functions with in the user defined sub function [UserForm_Init()]?

Chandra Sekhar K
  • 317
  • 7
  • 18
  • 2
    **Your code is not VBA**, and it's hard to determine what you're trying to do without a clear picture. (See "[mcve]".) For now, see [this search](https://www.google.com/search?q=private+public+declaration+vba) as well as [this Stack Overflow question](https://stackoverflow.com/q/3815547/8112776). – ashleedawg May 29 '18 at 14:53
  • @ashleedawg I have already written code for CheckBox and Radio buttons when i have checked or Enabled. So, next time when i opening the same userform, i want to run the same code, so i want to do that by calling the event functions directly. Which i am not able to do that and showing compile error. – Chandra Sekhar K May 29 '18 at 14:57
  • As stated in my answer, the correct way to do it is to put shared code in a shared module. I understand your code is written *may* require some changes, but it's part of the learning process. – ashleedawg May 29 '18 at 15:05
  • @ashleedwag I am sorry, I have uploaded code in C - format. I have corrected the code – Chandra Sekhar K May 29 '18 at 17:07

2 Answers2

2

VBA doesn't use curly braces. This code won't compile with those. VBA requires Sub and End Sub to define the procedures.

Sub CheckBox1_Click()

  if CheckBox1.Value Then
     --- Code
  end if
End Sub

Sub CheckBox2_Click()

  if CheckBox2.Value Then
     --- Code
  end if
End Sub

Sub RadioButton1_Click()

  if RadioButton1.Value Then
     --- Code
  End If
End Sub


Sub UserForm_Init()
 CheckBox1_Click()
 CheckBox2_Click()
 RadioButton1_Click()
End Sub

NOTE: If your custom function UserForm_Init is not in the Form's code module, you'll need to qualify the procedure names, e.g.:

Sub UserForm_Init()
 UserForm1.CheckBox1_Click()
 UserForm1.CheckBox2_Click()
 UserForm1.RadioButton1_Click()
End Sub

Additional complexity may arise based on your implementation, but I hope this will get you started.

David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • 1
    Thanks, it worked. I have forget to call the event functions by UserForm1.EventFunction(). Making the Event Functions Public is also important to call the functions in other functions. – Chandra Sekhar K May 30 '18 at 04:51
1

You're missing parts of code in your example; it's hard to determine what you're trying to do without a clear picture. It is possible to call an event procedure from another, it's a bad idea for a number of reasons.

Instead of calling a private procedure like you're trying to, sort of like this:

Private Sub myEvent()
    'event code here
End Sub

Sub mySub()
    myEvent()
End Sub

...you're better off to move the code that you need to be shared to a shared module, kind of like this:

Public Sub mySubInSharedModule()
    'event code here
End Sub

Private Sub myEvent()
    Call mySubInSharedModule()
End Sub

Public Sub mySub()
    Call mySubInSharedModule()
End Sub

There are numerous articles explaining this further, on this site and others. To get you started, see this search as well as this Stack Overflow question.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105