Use of Function

H

H.A. de Wilde

L.S.,

I have an Excel Workbook with a VBA Project containing 2 User Forms
with each a Command Button.

User Form 1:
'*************************
Option Explicit

Private Sub CmdUserForm1_Click()
ShowMessage
End Sub

Function ShowMessage()
MsgBox "test"
End Function
'*************************

Clicking on Command Button CmdUserForm1 results correctly in a Message
Box
showing the text "test" using the Funcion ShowMessage.

I like to use the Function ShowMessage also with User Form 2.

User Form 2:
'*************************
Option Explicit

Private Sub CmdUserForm2_Click()
ShowMessage
End Sub
'*************************

Unfortunately clicking on Command Button CmdUserForm2 results in an
Error Message: "Sub or Function not defined"

Of course I can add the Function also to User Form 2:

'*************************
Option Explicit

Private Sub CmdUserForm2_Click()
ShowMessage
End Sub

Function ShowMessage()
MsgBox "test"
End Function
'*************************

but does there exist a method to use the above mentioned Function with
both User Forms without adding the Function to both User Forms?
 
Z

Zack Barresse

Hello,

Put your function in a Standard Module, declare it publically, and change it
to a Sub rather than a Function. I.e.

Public Sub ShowMessage()
Msgbox "test"
End sub

HTH


--
Regards,
Zack Barresse, aka firefytr, (GT = TFS FF Zack)
To email, remove the NO SPAM. Please keep correspondence to the board, as
to benefit others.


"H.A. de Wilde" <[email protected]>
wrote in message
news:[email protected]...
 
H

H.A. de Wilde

Dear Zack and Toppers,

thank you both for yor help.
It works good now.

Kind Regards,

Hugo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top