Solved

I need a VBA event to call a function with parameters

Posted on 2011-09-08
6
228 Views
Last Modified: 2012-05-12
What's the VBA syntax (in Excel 2007) to have an event call an internal VBA function I will write, passing in a few parameters. I want to resuse that function for  multiple events and to use the input parameter to differentiate calls to the same function from various events?

Thanks.
0
Comment
Question by:newbieweb
  • 3
  • 3
6 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
ID: 36504162
Write the function like so:

Public Function Foo(ByVal RequestCode As Long, ByVal Variable1 As Variant, ByVal Variable2 As Variant) As Variant

    Select Case RequestCode
        Case 1:
            ' Do this
        Case 2:
            ' Do that
    End Select

End Function

Kevin
0
 

Author Comment

by:newbieweb
ID: 36504201
How do I execute it?
0
 

Author Comment

by:newbieweb
ID: 36504203
And what does "As Variant" do?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 500 total points
ID: 36504213
Like so:

Public Sub SomeEventHandler()

    Dim MyResult As Variant

    MyResult = Foo(1, "A", 100)

End Sub

Kevin
0
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 500 total points
ID: 36504226
"As Variant" defines the variable as a variant data type which means it can store any type of value.

You can also define a variable as Long, String, Date, an array, a collection, or any of many other types.

Kevin
0
 

Author Closing Comment

by:newbieweb
ID: 36504246
Thanks a lot!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you how to use shortcut menus in the Access run-time environment.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now