Solved

I need a VBA event to call a function with parameters

Posted on 2011-09-08
6
238 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Outlook Free & Paid Tools
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

772 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