VBA: Using VLOOKUP formula with a dynamic lookup_value
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 |
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 '18 at 1:40
Please consider pasting your code and explaining a bit more generally what the objective might be.
– Display name
Nov 20 '18 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 '18 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 '18 at 1:57
add a comment |
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 '18 at 1:55
Tim Williams
87.1k97086
87.1k97086
asked Nov 20 '18 at 1:16
marthebearmarthebear
111
111
Please add your code, so someone can help you. Provide any error that you get.
– GMalc
Nov 20 '18 at 1:40
Please consider pasting your code and explaining a bit more generally what the objective might be.
– Display name
Nov 20 '18 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 '18 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 '18 at 1:57
add a comment |
Please add your code, so someone can help you. Provide any error that you get.
– GMalc
Nov 20 '18 at 1:40
Please consider pasting your code and explaining a bit more generally what the objective might be.
– Display name
Nov 20 '18 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 '18 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 '18 at 1:57
Please add your code, so someone can help you. Provide any error that you get.
– GMalc
Nov 20 '18 at 1:40
Please add your code, so someone can help you. Provide any error that you get.
– GMalc
Nov 20 '18 at 1:40
Please consider pasting your code and explaining a bit more generally what the objective might be.
– Display name
Nov 20 '18 at 1:46
Please consider pasting your code and explaining a bit more generally what the objective might be.
– Display name
Nov 20 '18 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 '18 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 '18 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 '18 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 '18 at 1:57
add a comment |
1 Answer
1
active
oldest
votes
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 '18 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 '18 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 '18 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 '18 at 15:06
Or useTZsrchRange.Address(External:=True)
to return the full range reference without needing to manually construct it.
– Patrick Wynne
Nov 20 '18 at 20:57
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 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 '18 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 '18 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 '18 at 15:06
Or useTZsrchRange.Address(External:=True)
to return the full range reference without needing to manually construct it.
– Patrick Wynne
Nov 20 '18 at 20:57
|
show 1 more comment
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 '18 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 '18 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 '18 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 '18 at 15:06
Or useTZsrchRange.Address(External:=True)
to return the full range reference without needing to manually construct it.
– Patrick Wynne
Nov 20 '18 at 20:57
|
show 1 more comment
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 '18 at 3:55
answered Nov 20 '18 at 2:56
usmanhaqusmanhaq
1,113128
1,113128
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 '18 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 '18 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 '18 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 '18 at 15:06
Or useTZsrchRange.Address(External:=True)
to return the full range reference without needing to manually construct it.
– Patrick Wynne
Nov 20 '18 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 '18 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 '18 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 '18 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 '18 at 15:06
Or useTZsrchRange.Address(External:=True)
to return the full range reference without needing to manually construct it.
– Patrick Wynne
Nov 20 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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.
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 '18 at 1:40
Please consider pasting your code and explaining a bit more generally what the objective might be.
– Display name
Nov 20 '18 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 '18 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 '18 at 1:57