Solved

I need a VBA event to call a function with parameters

Posted on 2011-09-08
6
244 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

830 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