VBA: Using VLOOKUP formula with a dynamic lookup_value
up vote
1
down vote
favorite
I am trying to embed a VLOOKUP formula into my last column after performing formatting on a sheet. My lookup value is for a column that has the following column header: "Company State".
This is my current flow:
Find the Column number with the Column Header: "Company State":
CompanyStateColumnNumber = WorksheetFunction.Match("Company State", ws.Range("1:1"), 0)
Declare the range for lookup. In this case, it exists in a worksheet/workbook that is not the active workbook/worksheet. Hence, I am referencing it using ( I have already declared TZsrcRange as a Range type):
Set TZsrchRange = TemplateWS.Range("A:C")
Now, I am trying to embed the vlookup into my last column (I found my last column in the sheet and it is stored in the variable " NewestLCol". I want to embed it into the column until the very last row (stored in variable "LastRow") :
For x = 2 To LastRow
Set dynamic_lookup_value = Cells(x, CompanyStateColumnNumber)
Cells(x, NewestLCol).Formula = " =VLOOKUP(" & _
dynamic_lookup_value.Address(0, 0) & _
", Templatews.Range(A:C).Address(0,0),3, FALSE)"
Next x
Now, when I run it, it doesn't embed the formula and doesn't give me a value. What could my issue be?
excel vba excel-vba vlookup
add a comment |
up vote
1
down vote
favorite
I am trying to embed a VLOOKUP formula into my last column after performing formatting on a sheet. My lookup value is for a column that has the following column header: "Company State".
This is my current flow:
Find the Column number with the Column Header: "Company State":
CompanyStateColumnNumber = WorksheetFunction.Match("Company State", ws.Range("1:1"), 0)
Declare the range for lookup. In this case, it exists in a worksheet/workbook that is not the active workbook/worksheet. Hence, I am referencing it using ( I have already declared TZsrcRange as a Range type):
Set TZsrchRange = TemplateWS.Range("A:C")
Now, I am trying to embed the vlookup into my last column (I found my last column in the sheet and it is stored in the variable " NewestLCol". I want to embed it into the column until the very last row (stored in variable "LastRow") :
For x = 2 To LastRow
Set dynamic_lookup_value = Cells(x, CompanyStateColumnNumber)
Cells(x, NewestLCol).Formula = " =VLOOKUP(" & _
dynamic_lookup_value.Address(0, 0) & _
", Templatews.Range(A:C).Address(0,0),3, FALSE)"
Next x
Now, when I run it, it doesn't embed the formula and doesn't give me a value. What could my issue be?
excel vba excel-vba vlookup
Please add your code, so someone can help you. Provide any error that you get.
– GMalc
Nov 20 at 1:40
Please consider pasting your code and explaining a bit more generally what the objective might be.
– O.PAL
Nov 20 at 1:46
Also run your code step by step using the F8 key, ensuring your code indeed gets to the Formula assignation. If an error occurs, state its number and exact wording in the question.
– Excelosaurus
Nov 20 at 1:49
YourCells
calls need to be qualified with a worksheet object to be sure you're referencing the sheet you expect.
– Tim Williams
Nov 20 at 1:57
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to embed a VLOOKUP formula into my last column after performing formatting on a sheet. My lookup value is for a column that has the following column header: "Company State".
This is my current flow:
Find the Column number with the Column Header: "Company State":
CompanyStateColumnNumber = WorksheetFunction.Match("Company State", ws.Range("1:1"), 0)
Declare the range for lookup. In this case, it exists in a worksheet/workbook that is not the active workbook/worksheet. Hence, I am referencing it using ( I have already declared TZsrcRange as a Range type):
Set TZsrchRange = TemplateWS.Range("A:C")
Now, I am trying to embed the vlookup into my last column (I found my last column in the sheet and it is stored in the variable " NewestLCol". I want to embed it into the column until the very last row (stored in variable "LastRow") :
For x = 2 To LastRow
Set dynamic_lookup_value = Cells(x, CompanyStateColumnNumber)
Cells(x, NewestLCol).Formula = " =VLOOKUP(" & _
dynamic_lookup_value.Address(0, 0) & _
", Templatews.Range(A:C).Address(0,0),3, FALSE)"
Next x
Now, when I run it, it doesn't embed the formula and doesn't give me a value. What could my issue be?
excel vba excel-vba vlookup
I am trying to embed a VLOOKUP formula into my last column after performing formatting on a sheet. My lookup value is for a column that has the following column header: "Company State".
This is my current flow:
Find the Column number with the Column Header: "Company State":
CompanyStateColumnNumber = WorksheetFunction.Match("Company State", ws.Range("1:1"), 0)
Declare the range for lookup. In this case, it exists in a worksheet/workbook that is not the active workbook/worksheet. Hence, I am referencing it using ( I have already declared TZsrcRange as a Range type):
Set TZsrchRange = TemplateWS.Range("A:C")
Now, I am trying to embed the vlookup into my last column (I found my last column in the sheet and it is stored in the variable " NewestLCol". I want to embed it into the column until the very last row (stored in variable "LastRow") :
For x = 2 To LastRow
Set dynamic_lookup_value = Cells(x, CompanyStateColumnNumber)
Cells(x, NewestLCol).Formula = " =VLOOKUP(" & _
dynamic_lookup_value.Address(0, 0) & _
", Templatews.Range(A:C).Address(0,0),3, FALSE)"
Next x
Now, when I run it, it doesn't embed the formula and doesn't give me a value. What could my issue be?
excel vba excel-vba vlookup
excel vba excel-vba vlookup
edited Nov 20 at 1:55
Tim Williams
84.4k96583
84.4k96583
asked Nov 20 at 1:16
marthebear
111
111
Please add your code, so someone can help you. Provide any error that you get.
– GMalc
Nov 20 at 1:40
Please consider pasting your code and explaining a bit more generally what the objective might be.
– O.PAL
Nov 20 at 1:46
Also run your code step by step using the F8 key, ensuring your code indeed gets to the Formula assignation. If an error occurs, state its number and exact wording in the question.
– Excelosaurus
Nov 20 at 1:49
YourCells
calls need to be qualified with a worksheet object to be sure you're referencing the sheet you expect.
– Tim Williams
Nov 20 at 1:57
add a comment |
Please add your code, so someone can help you. Provide any error that you get.
– GMalc
Nov 20 at 1:40
Please consider pasting your code and explaining a bit more generally what the objective might be.
– O.PAL
Nov 20 at 1:46
Also run your code step by step using the F8 key, ensuring your code indeed gets to the Formula assignation. If an error occurs, state its number and exact wording in the question.
– Excelosaurus
Nov 20 at 1:49
YourCells
calls need to be qualified with a worksheet object to be sure you're referencing the sheet you expect.
– Tim Williams
Nov 20 at 1:57
Please add your code, so someone can help you. Provide any error that you get.
– GMalc
Nov 20 at 1:40
Please add your code, so someone can help you. Provide any error that you get.
– GMalc
Nov 20 at 1:40
Please consider pasting your code and explaining a bit more generally what the objective might be.
– O.PAL
Nov 20 at 1:46
Please consider pasting your code and explaining a bit more generally what the objective might be.
– O.PAL
Nov 20 at 1:46
Also run your code step by step using the F8 key, ensuring your code indeed gets to the Formula assignation. If an error occurs, state its number and exact wording in the question.
– Excelosaurus
Nov 20 at 1:49
Also run your code step by step using the F8 key, ensuring your code indeed gets to the Formula assignation. If an error occurs, state its number and exact wording in the question.
– Excelosaurus
Nov 20 at 1:49
Your
Cells
calls need to be qualified with a worksheet object to be sure you're referencing the sheet you expect.– Tim Williams
Nov 20 at 1:57
Your
Cells
calls need to be qualified with a worksheet object to be sure you're referencing the sheet you expect.– Tim Williams
Nov 20 at 1:57
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Try replacing your formula assignment with this one
Cells(x, NewestLCol).Formula = "=VLOOKUP(" & _
dynamic_lookup_value.Address(0, 0) & _
"," & _
TZsrchRange.Address(External:=True) & _
",3, FALSE)"
This formula actually embedded the vlookup into my cells. However, the reference was not to the other sheet (non active sheet). Is there a way to access that sheet for the lookup table? thank you so much usman!
– marthebear
Nov 20 at 4:01
You can replace the TZsrchRange.Address with other sheet's range reference. tell me which one you want to have
– usmanhaq
Nov 20 at 4:17
I thought I did that through Set TZsrchRange = TemplateWS.Range("A:C")? Essentially, I set TemplateWS=TemplateWB.Sheets("Sheet2") & TemplateWB=Workbooks("Template.xlsm")
– marthebear
Nov 20 at 14:46
You have to concatenate the full reference to the range yourself, the Address() function only returns the address of the range. something like "'[ " & file_name & "]Sheet2'!" & TZsrchRange.Address
– usmanhaq
Nov 20 at 15:06
Or useTZsrchRange.Address(External:=True)
to return the full range reference without needing to manually construct it.
– Patrick Wynne
Nov 20 at 20:57
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Try replacing your formula assignment with this one
Cells(x, NewestLCol).Formula = "=VLOOKUP(" & _
dynamic_lookup_value.Address(0, 0) & _
"," & _
TZsrchRange.Address(External:=True) & _
",3, FALSE)"
This formula actually embedded the vlookup into my cells. However, the reference was not to the other sheet (non active sheet). Is there a way to access that sheet for the lookup table? thank you so much usman!
– marthebear
Nov 20 at 4:01
You can replace the TZsrchRange.Address with other sheet's range reference. tell me which one you want to have
– usmanhaq
Nov 20 at 4:17
I thought I did that through Set TZsrchRange = TemplateWS.Range("A:C")? Essentially, I set TemplateWS=TemplateWB.Sheets("Sheet2") & TemplateWB=Workbooks("Template.xlsm")
– marthebear
Nov 20 at 14:46
You have to concatenate the full reference to the range yourself, the Address() function only returns the address of the range. something like "'[ " & file_name & "]Sheet2'!" & TZsrchRange.Address
– usmanhaq
Nov 20 at 15:06
Or useTZsrchRange.Address(External:=True)
to return the full range reference without needing to manually construct it.
– Patrick Wynne
Nov 20 at 20:57
|
show 1 more comment
up vote
0
down vote
Try replacing your formula assignment with this one
Cells(x, NewestLCol).Formula = "=VLOOKUP(" & _
dynamic_lookup_value.Address(0, 0) & _
"," & _
TZsrchRange.Address(External:=True) & _
",3, FALSE)"
This formula actually embedded the vlookup into my cells. However, the reference was not to the other sheet (non active sheet). Is there a way to access that sheet for the lookup table? thank you so much usman!
– marthebear
Nov 20 at 4:01
You can replace the TZsrchRange.Address with other sheet's range reference. tell me which one you want to have
– usmanhaq
Nov 20 at 4:17
I thought I did that through Set TZsrchRange = TemplateWS.Range("A:C")? Essentially, I set TemplateWS=TemplateWB.Sheets("Sheet2") & TemplateWB=Workbooks("Template.xlsm")
– marthebear
Nov 20 at 14:46
You have to concatenate the full reference to the range yourself, the Address() function only returns the address of the range. something like "'[ " & file_name & "]Sheet2'!" & TZsrchRange.Address
– usmanhaq
Nov 20 at 15:06
Or useTZsrchRange.Address(External:=True)
to return the full range reference without needing to manually construct it.
– Patrick Wynne
Nov 20 at 20:57
|
show 1 more comment
up vote
0
down vote
up vote
0
down vote
Try replacing your formula assignment with this one
Cells(x, NewestLCol).Formula = "=VLOOKUP(" & _
dynamic_lookup_value.Address(0, 0) & _
"," & _
TZsrchRange.Address(External:=True) & _
",3, FALSE)"
Try replacing your formula assignment with this one
Cells(x, NewestLCol).Formula = "=VLOOKUP(" & _
dynamic_lookup_value.Address(0, 0) & _
"," & _
TZsrchRange.Address(External:=True) & _
",3, FALSE)"
edited Nov 21 at 3:55
answered Nov 20 at 2:56
usmanhaq
1,083128
1,083128
This formula actually embedded the vlookup into my cells. However, the reference was not to the other sheet (non active sheet). Is there a way to access that sheet for the lookup table? thank you so much usman!
– marthebear
Nov 20 at 4:01
You can replace the TZsrchRange.Address with other sheet's range reference. tell me which one you want to have
– usmanhaq
Nov 20 at 4:17
I thought I did that through Set TZsrchRange = TemplateWS.Range("A:C")? Essentially, I set TemplateWS=TemplateWB.Sheets("Sheet2") & TemplateWB=Workbooks("Template.xlsm")
– marthebear
Nov 20 at 14:46
You have to concatenate the full reference to the range yourself, the Address() function only returns the address of the range. something like "'[ " & file_name & "]Sheet2'!" & TZsrchRange.Address
– usmanhaq
Nov 20 at 15:06
Or useTZsrchRange.Address(External:=True)
to return the full range reference without needing to manually construct it.
– Patrick Wynne
Nov 20 at 20:57
|
show 1 more comment
This formula actually embedded the vlookup into my cells. However, the reference was not to the other sheet (non active sheet). Is there a way to access that sheet for the lookup table? thank you so much usman!
– marthebear
Nov 20 at 4:01
You can replace the TZsrchRange.Address with other sheet's range reference. tell me which one you want to have
– usmanhaq
Nov 20 at 4:17
I thought I did that through Set TZsrchRange = TemplateWS.Range("A:C")? Essentially, I set TemplateWS=TemplateWB.Sheets("Sheet2") & TemplateWB=Workbooks("Template.xlsm")
– marthebear
Nov 20 at 14:46
You have to concatenate the full reference to the range yourself, the Address() function only returns the address of the range. something like "'[ " & file_name & "]Sheet2'!" & TZsrchRange.Address
– usmanhaq
Nov 20 at 15:06
Or useTZsrchRange.Address(External:=True)
to return the full range reference without needing to manually construct it.
– Patrick Wynne
Nov 20 at 20:57
This formula actually embedded the vlookup into my cells. However, the reference was not to the other sheet (non active sheet). Is there a way to access that sheet for the lookup table? thank you so much usman!
– marthebear
Nov 20 at 4:01
This formula actually embedded the vlookup into my cells. However, the reference was not to the other sheet (non active sheet). Is there a way to access that sheet for the lookup table? thank you so much usman!
– marthebear
Nov 20 at 4:01
You can replace the TZsrchRange.Address with other sheet's range reference. tell me which one you want to have
– usmanhaq
Nov 20 at 4:17
You can replace the TZsrchRange.Address with other sheet's range reference. tell me which one you want to have
– usmanhaq
Nov 20 at 4:17
I thought I did that through Set TZsrchRange = TemplateWS.Range("A:C")? Essentially, I set TemplateWS=TemplateWB.Sheets("Sheet2") & TemplateWB=Workbooks("Template.xlsm")
– marthebear
Nov 20 at 14:46
I thought I did that through Set TZsrchRange = TemplateWS.Range("A:C")? Essentially, I set TemplateWS=TemplateWB.Sheets("Sheet2") & TemplateWB=Workbooks("Template.xlsm")
– marthebear
Nov 20 at 14:46
You have to concatenate the full reference to the range yourself, the Address() function only returns the address of the range. something like "'[ " & file_name & "]Sheet2'!" & TZsrchRange.Address
– usmanhaq
Nov 20 at 15:06
You have to concatenate the full reference to the range yourself, the Address() function only returns the address of the range. something like "'[ " & file_name & "]Sheet2'!" & TZsrchRange.Address
– usmanhaq
Nov 20 at 15:06
Or use
TZsrchRange.Address(External:=True)
to return the full range reference without needing to manually construct it.– Patrick Wynne
Nov 20 at 20:57
Or use
TZsrchRange.Address(External:=True)
to return the full range reference without needing to manually construct it.– Patrick Wynne
Nov 20 at 20:57
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384885%2fvba-using-vlookup-formula-with-a-dynamic-lookup-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please add your code, so someone can help you. Provide any error that you get.
– GMalc
Nov 20 at 1:40
Please consider pasting your code and explaining a bit more generally what the objective might be.
– O.PAL
Nov 20 at 1:46
Also run your code step by step using the F8 key, ensuring your code indeed gets to the Formula assignation. If an error occurs, state its number and exact wording in the question.
– Excelosaurus
Nov 20 at 1:49
Your
Cells
calls need to be qualified with a worksheet object to be sure you're referencing the sheet you expect.– Tim Williams
Nov 20 at 1:57