?
Solved

Pass worksheet name to Vlookup function in Excel or VB

Posted on 2010-09-10
18
Medium Priority
?
860 Views
Last Modified: 2013-11-26
I would like to know if there is a way to pass a worksheet name to a VLookup function in Excel or using VB code.  

=VLOOKUP(INDIRECT(Worksheet_Name!$C$1),Worksheet_Name!INFOTAB,1,FALSE)

0
Comment
Question by:morinia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
  • +3
18 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33646632
Can you take another crack at explaining your question, and perhaps illustrate it with a sample file?
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 33646650
I think he means something like:

=VLOOKUP(INDIRECT(<ValueOfCellA1Here>!$C$1),Worksheet_Name!INFOTAB,1,FALSE)

Dunno if you can do it by excel, but by VBA it's easy:

Cells(1,1).Formula="=VLOOKUP(INDIRECT(" & strSheetVar & "!$C$1),Worksheet_Name!INFOTAB,1,FALSE)"

This writes to cell A1 and looks up in whatever sheet you have in your variable.
0
 
LVL 17

Expert Comment

by:wobbled
ID: 33646651
This is how you get the worksheet name in VBA

Public Function WkShtName() As String
   
    WkShtName = ActiveSheet.Name

End Function

By making it a public function you can directly refer to it from the Excel Sheet eg =WkShtName
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:Cluskitt
ID: 33646657
Err... replace the second worksheet_name as well:

Cells(1,1).Formula="=VLOOKUP(INDIRECT(" & strSheetVar & "!$C$1)," & strSheetVar & "!INFOTAB,1,FALSE)"
0
 
LVL 13

Expert Comment

by:MWGainesJR
ID: 33646690
this formula will return the sheetname: (http://www.exceltip.com/st/Cell_Function_Returns_Sheet_Name,_Workbook_Name_and_Path/180.html)

=MID(CELL("filename"),FIND("]",CELL("filename"))+1,255)
so in your case,  you formula would be:

 =VLOOKUP(INDIRECT(MID(CELL("filename"),FIND("]",CELL("filename"))+1,255)!$C$1),MID(CELL("filename"),FIND("]",CELL("filename"))+1,255)!INFOTAB,1,FALSE)

0
 

Author Comment

by:morinia
ID: 33646718
The following statement is a named field in Excel.

=VLOOKUP(INDIRECT(Total_Authorizations!$C$1),Total_Authorizations!INFOTAB,1,FALSE

If possible, I would like to use the same named field for four tables.  The problem I am having is that the table name automatically is put in when I save the formula as shown above.  When I create the formula it looks like this

=VLOOKUP(INDIRECT($C$1),INFOTAB,1,FALSE

Upon saving it adds the Table Name.  Unfortunately this gives erronous results when I try to use it on a different worksheet.  However, when on that worksheet.

Because all files are made public, I cannot post the file.

I am willing to change the code to VB and create a user defined function if that is the only way.
0
 
LVL 13

Expert Comment

by:MWGainesJR
ID: 33646745
morinia,
I gave you the formula need above without using VBA.
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33646769
Using the CELL function, as per MWGainesJR above, works, but only as long as the file has already been saved; try entering that before the workbook has been saved, and you get a #VALUE error.

However, as far as I know, the CELL function is the only non-VBA way to get at this.

If you go the UDF route, I strongly recommend *not* keying on the ActiveSheet.  For example, I would be more inclined to use something like the following:




Public Function WkShtName(Optional Rng As Range) As String
   
    If Rng Is Nothing Then
        WkShtName = Application.Caller.Parent.Name
    Else
        WkShtName = Rng.Parent.Name
    End If

End Function



If you pass in a range reference, the function returns the worksheet name based on the parent of that range reference.  If you omit the range, the function returns the name of the parent of the cell containing the formula.
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33646791
>>Because all files are made public, I cannot post the file.

Then post a file with an identical structure, but with "fake" data.  Not hard to do, really.
0
 
LVL 13

Expert Comment

by:MWGainesJR
ID: 33646816
If the workbook isn't saved, I highly doubt it will have modules much less vba code
Unless of course you're gonna go all the way down the route of creating an xla or import the modules before attempting the lookup....
=)
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33647075
MW,

>>If the workbook isn't saved, I highly doubt it will have modules much less vba code

You've never created a new workbook, and then added code before saving it?

:)

In any event, no criticism of your method was implied--not intentionally, at least.  It is probably the only non-VBA approach available.

Patrick
0
 
LVL 13

Expert Comment

by:MWGainesJR
ID: 33647119
If the author is ok with adding code to every new workbook in order to do a vlookup rather than clicking "save", then by all means.....give'm the vba =)
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 33647300
=VLOOKUP(INDIRECT(INDIRECT("R1C3",0)),INDIRECT("INFOTAB"),1,FALSE)
unless INFOTAB is a dynamic named range?
0
 

Author Comment

by:morinia
ID: 33647397
matthewspatrick:

The code you gave returns the sheet name, how do I bring it into my formula.

=VLOOKUP(INDIRECT($C$1),INFOTAB,1,FALSE)
0
 

Author Comment

by:morinia
ID: 33647456
rorya

The formula worked perfectly.  Can you explain what the formula is doing for all of us.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 33647477
It replaces your two range references that are being converted with INDIRECT so that you can use strings (no conversion).
It wouldn't be my first choice though.
0
 

Author Comment

by:morinia
ID: 33647509
What would be your first choice?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 33647541
Something that doesn't involve INDIRECT or VLOOKUP, most likely.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

765 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