2

I want to pass a CommandButton as an argument.

Example:

Sub calc(btn as button) btn.Caption = "Something" End Sub Private Sub CommandButton1_Click() calc(CommandButton1) End Sub Private Sub CommandButton2_Click() calc(CommandButton2) End Sub 

Is something like the above possible? If yes how can I do it?

edit

Thanks for your response, but I dont get it. So it looks like this now:

Public Sub calc(ByRef btn as Object) btn.Caption = "Something" End Sub Private Sub CommandButton1_Click() calc(CommandButton1) End Sub Private Sub CommandButton2_Click() calc(CommandButton2) End Sub 

Maybe someone can explain it to me in more detail, because Im very new to VBA.

2 Answers 2

3

You need:

Sub calc(btn As MSForms.CommandButton) btn.Caption = "Something" End Sub 

And you must invoke it following the rules:

 calc CommandButton1 // best call calc (CommandButton1) // ok but verbose calc (CommandButton1) // type mismatch! 

(The type mismatch is because the parentheses evaluate CommandButton1 which results in its default property (a string) which is incompatible with the method argument type)

Sign up to request clarification or add additional context in comments.

Comments

0

This is the sub:

Public Sub temp(ByRef cmdb As Object) cmdb.Caption = "somethine else" End Sub 

This is how you would call it

call sub (commandbutton_1) 

4 Comments

Thanks for your response, but I dont get it. So it looks like this now: Public Sub calc(ByRef btn as Object) btn.Caption = "Something" End Sub Private Sub CommandButton1_Click() calc(CommandButton1) End Sub Private Sub CommandButton2_Click() calc(CommandButton2) End Sub Maybe someone can explain it to me in more detail, because Im very new to VBA.
changed it a bit, but don't how much easier it could explain it :), If it still doesn't make sense email me your file and I can take a look at it for you
thank you very much! It works. I need to read some more about the Call keyword.
np, glad I could help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.