+ Reply to Thread
Results 1 to 3 of 3

Thread: calling functions

  1. #1
    Registered User
    Join Date
    09-13-2011
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    9

    calling functions

    I'm trying to write a function that can be called by other functions, but i'm having trouble passing variables to it the orderStuff function would be called by many different command buttons. I need to pass the Tag property of the command button to the orderStuff Function. The itemLoc variable in orderStuff, never contains the value of the Tag property from the command button.

    Private Sub CommandButton1_Click()
    
        Dim itemLoc As Integer
        itemLoc = CommandButton1.Tag
    
        Call orderStuff
    
    End Sub


    Private Sub orderStuff()
    
        Dim menuItem As String
        Dim itemPrice As String
    
        menuItem = WorksheetFunction.VLookup(itemLoc, Sheets("Items").Range("itemNumbers"), 2)
        itemPrice = WorksheetFunction.VLookup(itemLoc, Sheets("Items").Range("itemNumbers"), 3)
        
        With Range("K5")
            .Insert Shift:=xlDown
            .Value = menuItem
        End With
        
        With Range("L5")
            .Insert Shift:=xlDown
            .Value = itemPrice
        End With
        
    End Sub
    Last edited by stevedomer; 09-15-2011 at 07:48 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: calling functions

    Hello stevedomer,

    Welcome to the Forum!

    I have modified your code to pass the itemLoc from the CommandButton to the orderStuff function. However, I do not see any code where you assign anything the CommandButton1's Tag property.
    Private Sub CommandButton1_Click()
    
        Dim itemLoc As Integer
        itemLoc = CommandButton1.Tag
    
        Call orderStuff(itemLoc)
    
    End Sub

    Private Sub orderStuff(ByVal itemLoc As String)
    
        Dim menuItem As String
        Dim itemPrice As String
    
        menuItem = WorksheetFunction.VLookup(itemLoc, Sheets("Items").Range("itemNumbers"), 2)
        itemPrice = WorksheetFunction.VLookup(itemLoc, Sheets("Items").Range("itemNumbers"), 3)
        
        With Range("K5")
            .Insert Shift:=xlDown
            .Value = menuItem
        End With
        
        With Range("L5")
            .Insert Shift:=xlDown
            .Value = itemPrice
        End With
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    09-13-2011
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: calling functions

    Thanks Leith,

    the Tag is static for each command button, so i set it in the properties pane.

    i did have to change Private Sub orderStuff(ByVal itemLoc As String) to Integer instead of String, otherwise i got an Unable to get the VLookup Property of the Worksheet Function Class.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0