Vlookup with variable sheet Name as a variant

MostafaNabil

New Member
Joined
Feb 20, 2023
Messages
8
Office Version
  1. 2019
Platform
  1. Windows
I'm working on a code and everything is working fine but I need to set vlookup value from a variable sheet Name with the same structure

I tried many syntax with no use any advise.

This a portion of the code which is related to my issue.

Dim SheetName As Variant
SheetName= DestinationWs.Name

Dim StringVal As String
StringVal = CStr(SheetName)

DestinationWs.Range("L4").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-2],INDIRECT(""'"" & StringVal & ""'!"" & " R[" & OldRemainingLastRowFirst & "]C[12] : R[" & OldRemainingLastRowFirst & "]C[14]),2,0)"
 
That photo cannot be the result of running my last code.
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
That photo cannot be the result of running my last code.
1676933420763.png


This is the result of your code.
 
Upvote 0
Yes, and that formula was not present in the photo you posted.
 
Upvote 0
Yes, and that formula was not present in the photo you posted.

So, could you help me please with the following issue after updating my code.
as The following code portion should perform Vlookup with variable sheetname. However, the vlookup returns only 0's not the desired value from the lookup range, considering that the lookup range has the same structure across these multiple sheets with variable name, so any advise.

VBA Code:
Dim SheetName As Variant: SheetName = DestinationWs.Name

Dim OldRemainingHoursLastRowFirst As Variant
OldRemainingHoursLastRowFirst = DestinationWs.Range("W3").End(xlDown).Row

DestinationWs.Range("L4").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-2],""'" & StringVal & "'!R4C12:R" & OldRemainingHoursLastRowFirst & "C14"",2,0),0)"

Unfortunately, the following uploaded photo is the result of the code.

1676935659459.png
 
Upvote 0
Why did you take the INDIRECT part out of the formula?
 
Upvote 0

Forum statistics

Threads
1,215,170
Messages
6,123,422
Members
449,099
Latest member
COOT

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