Referencing a sheet by its order or index number in a Vlookup formula

Wheelie_Awesome

New Member
Joined
Aug 18, 2021
Messages
4
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
I would like to have a macro with vlookups for a workbook that I add weekly sheets to by date. So Sheet1 is current date, Sheet2 is last weeks date. Then next week I'll add another new Sheet1, and so on. I want my vlookup to also look in Sheet2, rather than the name of the sheet.

I've been messing around by making a new module with:
Function SHEETNAME(number As Long) As String
SHEETNAME = Sheets(number).Name
End Function

And can get a correctly returned result for summing up a column. =SUM(INDIRECT("'"&SHEETNAME(2) &"'!S:S"))
But I can't figure out how to get the Vlookup to work. Is this even possible?

My regular vlookup formula would look like this: =VLOOKUP($C3,'8-11-2021'!$C:$G,2,0)
Where 8-11-2021 is the sheet name that I want to reference just as Sheet2.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi, welcome to the forum!

You could try like this.

Excel Formula:
=VLOOKUP($C3,INDIRECT("'"&SHEETNAME(2)&"'!C:G"),2,0)
 
Upvote 0
That did it! Thank you so much. :)
Hi, I'm happy you got it working! I'm running into the same exact issue with one of my workbooks, but unfortunately I was not able to get it working. I'm trying to put the vlookup on cell L3 of sheet 1 and pull info from sheet 2 range F through M. I've pasted my module below, how can I get this working?

VBA Code:
Function SHEETNAME(number As Long) As String
SHEETNAME = Sheets(number).Name
End Function

Sub VLookUp()
    Range("L3").Select
    ActiveCell.FormulaR1C1 = _
        "=VLOOKUP($F3,INDIRECT(" '"&SHEETNAME(2)&"'!F:M"),7,0)"
    Range("L3").Select
    Selection.AutoFill Destination:=Range("L3:L1000")
    Range("A1").Select
End Sub
 
Upvote 0
how can I get this working?

Hi, welcome to the forum!

You could try like this (note how I've used a different name for the macro - you should really avoid using names that already have a meaning in Excel or VBA).

VBA Code:
Sub myVLookUp()
Range("L3:L1000").Formula = "=VLOOKUP($F3,INDIRECT(""'""&SHEETNAME(2)&""'!F:M""),7,0)"
End Sub

Or as you are applying the formula via code, you could possibly dispense with the indirect function and UDF altogether and just use.

VBA Code:
Sub myVLookUp()
 Range("L3:L1000").Formula = "=VLOOKUP($F3,'" & Sheets(2).Name & "'!F:M,7,0)"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,635
Messages
6,120,660
Members
448,975
Latest member
sweeberry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top