Quantcast
Channel: Executing code automatically
Viewing all articles
Browse latest Browse all 2

Executing code automatically

0
0

I wrote some code within a UserForm that does xyz when a command button in the UserForm is clicked. I then created a macro that runs the code in the UserForm using the statement "UserForm1.Show". I then added a custom button to a toolbar that the user can click to run this macro.  The result is the user clicks a custom button on a toolbar and the UserForm appears, instructing the user to click a command button that will cause xyz to happen.

My question is how do I get xyz to happen without requiring the user to click a command button on the UserForm? I want xyz to be carried out as soon as the custom button on the toolbar is clicked. The intermediate step of clicking a command button is not needed. Does it have something to do with how the Sub that carries out xyz is named? Consider the following code;

 

Private Sub Label1_Click()
If InStr(1, Selection, "dog") Then
Label1.Caption = "Found an occurrence of the word dog"
Else
Label1.Caption = "Didn't find an occurrence of the word dog"
End If
End Sub

 

How could I alter this code so the click on the label is not required for the text search to be carried out? I want the user to be able to click the custom button on the toolbar and the Userform (or a MessageBox) would immediately appear with the result of the search. Instead of the Sub being named Label1_Click(), should it be called something like (at a wild guess) Label1_JustDoIt()?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images