?
Solved

Pass worksheet name to Vlookup function in Excel or VB

Posted on 2010-09-10
18
Medium Priority
?
901 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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

850 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