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.
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 Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks