VLookup function in VBA - Return a department value based on name valuevlookup with VBA to PERSONAL.xlsbVlookup - Find the Max/Min and return the word three columns to the rightCalling IfError with vLookUp in VBA returns error even though cell formula returns valueVLookup in VBA for loop failing after first passVlookup on value given in textbox1 & textbox2 & return value in another testbox3 from sheet2Excel, Compare values from different sheets, if they match return data from a third columnReturn MAX value with VLOOKUP from listVBA Vlookup not finding values that existI am using vlookup across 2 spreadsheets. The formula appears to be correct, but the value is not returning, only the formula displaysRE: VBA - (VLOOKUP) + (VALUE +1)
My employer is refusing to give me the pay that was advertised after an internal job move
Did Vladimir Lenin have a cat?
Desktop app status bar: Notification vs error message
How does a poisoned arrow combine with the spell Conjure Barrage?
Scam? Checks via Email
What is a good example for artistic ND filter applications?
Nuclear breeder/reactor plant controlled by two A.I. makes too much power
What would the United Kingdom's "optimal" Brexit deal look like?
Convert graph format for Mathematica graph functions
Why did House of Representatives need to condemn Trumps Tweets?
How did astronauts using rovers tell direction without compasses on the Moon?
Why are we moving in circles with a tandem kayak?
If you inherit a Roth 401(k), is it taxed?
Why is it "on the inside" and not "in the inside"?
Do 3/8 (37.5%) of Quadratics Have No x-Intercepts?
Can a machine benefit from stored heat energy?
How well would the Moon protect the Earth from an Asteroid?
Foucault pendulum historical question
Does dual boot harm a laptop battery or reduce its life?
Why does the Eurostar not show youth pricing?
Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?
Why were contact sensors put on three of the Lunar Module's four legs? Did they ever bend and stick out sideways?
Why tantalum for the Hayabusa bullets?
Is SecureRandom.ints() secure?
VLookup function in VBA - Return a department value based on name value
vlookup with VBA to PERSONAL.xlsbVlookup - Find the Max/Min and return the word three columns to the rightCalling IfError with vLookUp in VBA returns error even though cell formula returns valueVLookup in VBA for loop failing after first passVlookup on value given in textbox1 & textbox2 & return value in another testbox3 from sheet2Excel, Compare values from different sheets, if they match return data from a third columnReturn MAX value with VLOOKUP from listVBA Vlookup not finding values that existI am using vlookup across 2 spreadsheets. The formula appears to be correct, but the value is not returning, only the formula displaysRE: VBA - (VLOOKUP) + (VALUE +1)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am looking to return a department value based off a name value. The name list is imported from a third party vendor, which randomizes the order each time, so I cannot set a department value to a specified cell. I have a range of data (dim rng) which has a name and column A and a department in column B. I would like the department associated with the name to be returned with the same on the new sheet. I have attached my code but am getting application defined or object-defined error and I am not sure why.
Sub find()
Dim namerng As Range
Dim rng As Range
Dim depti As Integer
Dim dept As String
Set namerng = Range("A1:A250") 'range of names
Set rng = Sheet1.Range("A1:B250") 'range of names in col A and dept in col B
For depti = 3 To 250
dept = "=IfError(Vlookup(namerng,rng,2,FALSE), "")"
Range("B" & depti) = dept
Next
End Sub
excel vba range vlookup
|
show 4 more comments
I am looking to return a department value based off a name value. The name list is imported from a third party vendor, which randomizes the order each time, so I cannot set a department value to a specified cell. I have a range of data (dim rng) which has a name and column A and a department in column B. I would like the department associated with the name to be returned with the same on the new sheet. I have attached my code but am getting application defined or object-defined error and I am not sure why.
Sub find()
Dim namerng As Range
Dim rng As Range
Dim depti As Integer
Dim dept As String
Set namerng = Range("A1:A250") 'range of names
Set rng = Sheet1.Range("A1:B250") 'range of names in col A and dept in col B
For depti = 3 To 250
dept = "=IfError(Vlookup(namerng,rng,2,FALSE), "")"
Range("B" & depti) = dept
Next
End Sub
excel vba range vlookup
Which line are you getting the error on?
– Mistella
Mar 26 at 20:35
Also, if you're adding a VBA reference into a string equation for Excel, you need to add the variables like"=IfError(Vlookup(" & "namerng & "," & rng & ",2,FALSE), """")"
(Note the extra quotation marks to add them to the string. if you didDebug.Print "=IfError(Vlookup(namerng,rng,2,FALSE), "")"
, the output would be=IfError(Vlookup(namerng,rng,2,FALSE), ")
)
– Mistella
Mar 26 at 20:38
I am getting the error on the Range("B" & depti) = dept line
– Jpb104
Mar 26 at 20:40
Adding that debug.print got the program to run, but no values are in column B for the department
– Jpb104
Mar 26 at 20:43
I believe if you usedept = "=IfError(Vlookup(" & namerng.Address & "," & rng.Address & ",2,FALSE), """")"
, it should run and insert the equation. However, I'm not convinced that this equation is what you want. Right now, there are circular references (everything's happening on the same sheet), and VLOOKUP only accepts one value for the first param, not a range of multiple cells.
– Mistella
Mar 26 at 20:52
|
show 4 more comments
I am looking to return a department value based off a name value. The name list is imported from a third party vendor, which randomizes the order each time, so I cannot set a department value to a specified cell. I have a range of data (dim rng) which has a name and column A and a department in column B. I would like the department associated with the name to be returned with the same on the new sheet. I have attached my code but am getting application defined or object-defined error and I am not sure why.
Sub find()
Dim namerng As Range
Dim rng As Range
Dim depti As Integer
Dim dept As String
Set namerng = Range("A1:A250") 'range of names
Set rng = Sheet1.Range("A1:B250") 'range of names in col A and dept in col B
For depti = 3 To 250
dept = "=IfError(Vlookup(namerng,rng,2,FALSE), "")"
Range("B" & depti) = dept
Next
End Sub
excel vba range vlookup
I am looking to return a department value based off a name value. The name list is imported from a third party vendor, which randomizes the order each time, so I cannot set a department value to a specified cell. I have a range of data (dim rng) which has a name and column A and a department in column B. I would like the department associated with the name to be returned with the same on the new sheet. I have attached my code but am getting application defined or object-defined error and I am not sure why.
Sub find()
Dim namerng As Range
Dim rng As Range
Dim depti As Integer
Dim dept As String
Set namerng = Range("A1:A250") 'range of names
Set rng = Sheet1.Range("A1:B250") 'range of names in col A and dept in col B
For depti = 3 To 250
dept = "=IfError(Vlookup(namerng,rng,2,FALSE), "")"
Range("B" & depti) = dept
Next
End Sub
excel vba range vlookup
excel vba range vlookup
edited Mar 27 at 7:54
Pᴇʜ
30.4k6 gold badges30 silver badges56 bronze badges
30.4k6 gold badges30 silver badges56 bronze badges
asked Mar 26 at 20:29
Jpb104Jpb104
31 bronze badge
31 bronze badge
Which line are you getting the error on?
– Mistella
Mar 26 at 20:35
Also, if you're adding a VBA reference into a string equation for Excel, you need to add the variables like"=IfError(Vlookup(" & "namerng & "," & rng & ",2,FALSE), """")"
(Note the extra quotation marks to add them to the string. if you didDebug.Print "=IfError(Vlookup(namerng,rng,2,FALSE), "")"
, the output would be=IfError(Vlookup(namerng,rng,2,FALSE), ")
)
– Mistella
Mar 26 at 20:38
I am getting the error on the Range("B" & depti) = dept line
– Jpb104
Mar 26 at 20:40
Adding that debug.print got the program to run, but no values are in column B for the department
– Jpb104
Mar 26 at 20:43
I believe if you usedept = "=IfError(Vlookup(" & namerng.Address & "," & rng.Address & ",2,FALSE), """")"
, it should run and insert the equation. However, I'm not convinced that this equation is what you want. Right now, there are circular references (everything's happening on the same sheet), and VLOOKUP only accepts one value for the first param, not a range of multiple cells.
– Mistella
Mar 26 at 20:52
|
show 4 more comments
Which line are you getting the error on?
– Mistella
Mar 26 at 20:35
Also, if you're adding a VBA reference into a string equation for Excel, you need to add the variables like"=IfError(Vlookup(" & "namerng & "," & rng & ",2,FALSE), """")"
(Note the extra quotation marks to add them to the string. if you didDebug.Print "=IfError(Vlookup(namerng,rng,2,FALSE), "")"
, the output would be=IfError(Vlookup(namerng,rng,2,FALSE), ")
)
– Mistella
Mar 26 at 20:38
I am getting the error on the Range("B" & depti) = dept line
– Jpb104
Mar 26 at 20:40
Adding that debug.print got the program to run, but no values are in column B for the department
– Jpb104
Mar 26 at 20:43
I believe if you usedept = "=IfError(Vlookup(" & namerng.Address & "," & rng.Address & ",2,FALSE), """")"
, it should run and insert the equation. However, I'm not convinced that this equation is what you want. Right now, there are circular references (everything's happening on the same sheet), and VLOOKUP only accepts one value for the first param, not a range of multiple cells.
– Mistella
Mar 26 at 20:52
Which line are you getting the error on?
– Mistella
Mar 26 at 20:35
Which line are you getting the error on?
– Mistella
Mar 26 at 20:35
Also, if you're adding a VBA reference into a string equation for Excel, you need to add the variables like
"=IfError(Vlookup(" & "namerng & "," & rng & ",2,FALSE), """")"
(Note the extra quotation marks to add them to the string. if you did Debug.Print "=IfError(Vlookup(namerng,rng,2,FALSE), "")"
, the output would be =IfError(Vlookup(namerng,rng,2,FALSE), ")
)– Mistella
Mar 26 at 20:38
Also, if you're adding a VBA reference into a string equation for Excel, you need to add the variables like
"=IfError(Vlookup(" & "namerng & "," & rng & ",2,FALSE), """")"
(Note the extra quotation marks to add them to the string. if you did Debug.Print "=IfError(Vlookup(namerng,rng,2,FALSE), "")"
, the output would be =IfError(Vlookup(namerng,rng,2,FALSE), ")
)– Mistella
Mar 26 at 20:38
I am getting the error on the Range("B" & depti) = dept line
– Jpb104
Mar 26 at 20:40
I am getting the error on the Range("B" & depti) = dept line
– Jpb104
Mar 26 at 20:40
Adding that debug.print got the program to run, but no values are in column B for the department
– Jpb104
Mar 26 at 20:43
Adding that debug.print got the program to run, but no values are in column B for the department
– Jpb104
Mar 26 at 20:43
I believe if you use
dept = "=IfError(Vlookup(" & namerng.Address & "," & rng.Address & ",2,FALSE), """")"
, it should run and insert the equation. However, I'm not convinced that this equation is what you want. Right now, there are circular references (everything's happening on the same sheet), and VLOOKUP only accepts one value for the first param, not a range of multiple cells.– Mistella
Mar 26 at 20:52
I believe if you use
dept = "=IfError(Vlookup(" & namerng.Address & "," & rng.Address & ",2,FALSE), """")"
, it should run and insert the equation. However, I'm not convinced that this equation is what you want. Right now, there are circular references (everything's happening on the same sheet), and VLOOKUP only accepts one value for the first param, not a range of multiple cells.– Mistella
Mar 26 at 20:52
|
show 4 more comments
2 Answers
2
active
oldest
votes
It looks like you're missing some code to try and fulfill what you're wanting, so some of the below is an assumption.
Sub find()
Dim namerng As Range 'List of Randomly Organized Names Only
Dim Lookuprng As Range 'List of Names and Departments
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Set sh1 = ThisWorkbook.Worksheets(1) 'List of Randomly Organized Names Only
Set sh2 = ThisWorkbook.Worksheets(2) 'List of Names and Departments
'List of Names and Departments
sh1.Activate
Set Lookuprng = sh1.Range(Cells(1, 1), (Cells(Rows.Count, 2).End(xlUp)))
'List of Randomly Organized Names Only ******NEED TO SPECIFY WHERE THESE NAMES ARE COMING FROM, I used Sheet1
sh2.Activate
Set namerng = sh2.Range(Cells(1, 1), (Cells(Rows.Count, 1).End(xlUp)))
'Put correct department with name
namerng.Offset(0, 1) = Application.VLookup(namerng, Lookuprng, 2, False)
End Sub
Thank you so much for the comment! This worked very well for me, with the exception of a few tweaks just because, you are right, I picked a select part from my code to post so I had a few more things going along with it I had to account for. Thank you again!!
– Jpb104
Mar 27 at 20:17
You are welcome. If my answer worked for you, please mark it as the answer by clicking the check mark.
– W-hit
Mar 27 at 20:21
Just did! Sorry still new to this site haha
– Jpb104
Mar 27 at 21:25
add a comment |
Based on your description of the issue and the problem you want to solve, I believe that the following code should work as needed.
Sub find()
Dim rng As Range
Dim depti As Integer
Dim dept As String
Set rng = Sheet1.Range("A1:B250") 'range of names in col A and dept in col B
For depti = 3 To 250
dept = "=IfError(Vlookup(A" & depti & ",'" & Sheet1.Name & "'!" & rng.Address & ",2,FALSE), """")"
Range("B" & depti) = dept
Next
End Sub
Notes
I eliminated the namerng
variable, because we don't need it for the updated equation. Since VLookup only takes a single value as the first param, that value should be dependent on the value of the loop variable, same as the destination range is.
I also escaped the quotation marks, so that the function would be valid, and added in the sheet1.name, so that the range address will point to the correct sheet (with single quotation marks, in case the sheet name has a space in it).
A cautionary note: the way this is coded, the equation will be inserted on whichever sheet is the active one at the time of the code run. This means that if Sheet1
is active, the departments will get overwritten with a circular-formula.
W-hit's answer looks like it will fill in the actually department values, while mine will fill in an excel equation which returns them. (stackoverflow.com/a/55366184)
– Mistella
Mar 26 at 21:09
Thank you so much for the response! This was getting the program to run, but would not pull the values from the second sheet that I needed. I think it was an issue on my end however, as my formulas in the sheet got messed up along the line, and anyone who sees this in the future can use this to help them out
– Jpb104
Mar 27 at 20:15
add a 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%2f55365720%2fvlookup-function-in-vba-return-a-department-value-based-on-name-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like you're missing some code to try and fulfill what you're wanting, so some of the below is an assumption.
Sub find()
Dim namerng As Range 'List of Randomly Organized Names Only
Dim Lookuprng As Range 'List of Names and Departments
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Set sh1 = ThisWorkbook.Worksheets(1) 'List of Randomly Organized Names Only
Set sh2 = ThisWorkbook.Worksheets(2) 'List of Names and Departments
'List of Names and Departments
sh1.Activate
Set Lookuprng = sh1.Range(Cells(1, 1), (Cells(Rows.Count, 2).End(xlUp)))
'List of Randomly Organized Names Only ******NEED TO SPECIFY WHERE THESE NAMES ARE COMING FROM, I used Sheet1
sh2.Activate
Set namerng = sh2.Range(Cells(1, 1), (Cells(Rows.Count, 1).End(xlUp)))
'Put correct department with name
namerng.Offset(0, 1) = Application.VLookup(namerng, Lookuprng, 2, False)
End Sub
Thank you so much for the comment! This worked very well for me, with the exception of a few tweaks just because, you are right, I picked a select part from my code to post so I had a few more things going along with it I had to account for. Thank you again!!
– Jpb104
Mar 27 at 20:17
You are welcome. If my answer worked for you, please mark it as the answer by clicking the check mark.
– W-hit
Mar 27 at 20:21
Just did! Sorry still new to this site haha
– Jpb104
Mar 27 at 21:25
add a comment |
It looks like you're missing some code to try and fulfill what you're wanting, so some of the below is an assumption.
Sub find()
Dim namerng As Range 'List of Randomly Organized Names Only
Dim Lookuprng As Range 'List of Names and Departments
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Set sh1 = ThisWorkbook.Worksheets(1) 'List of Randomly Organized Names Only
Set sh2 = ThisWorkbook.Worksheets(2) 'List of Names and Departments
'List of Names and Departments
sh1.Activate
Set Lookuprng = sh1.Range(Cells(1, 1), (Cells(Rows.Count, 2).End(xlUp)))
'List of Randomly Organized Names Only ******NEED TO SPECIFY WHERE THESE NAMES ARE COMING FROM, I used Sheet1
sh2.Activate
Set namerng = sh2.Range(Cells(1, 1), (Cells(Rows.Count, 1).End(xlUp)))
'Put correct department with name
namerng.Offset(0, 1) = Application.VLookup(namerng, Lookuprng, 2, False)
End Sub
Thank you so much for the comment! This worked very well for me, with the exception of a few tweaks just because, you are right, I picked a select part from my code to post so I had a few more things going along with it I had to account for. Thank you again!!
– Jpb104
Mar 27 at 20:17
You are welcome. If my answer worked for you, please mark it as the answer by clicking the check mark.
– W-hit
Mar 27 at 20:21
Just did! Sorry still new to this site haha
– Jpb104
Mar 27 at 21:25
add a comment |
It looks like you're missing some code to try and fulfill what you're wanting, so some of the below is an assumption.
Sub find()
Dim namerng As Range 'List of Randomly Organized Names Only
Dim Lookuprng As Range 'List of Names and Departments
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Set sh1 = ThisWorkbook.Worksheets(1) 'List of Randomly Organized Names Only
Set sh2 = ThisWorkbook.Worksheets(2) 'List of Names and Departments
'List of Names and Departments
sh1.Activate
Set Lookuprng = sh1.Range(Cells(1, 1), (Cells(Rows.Count, 2).End(xlUp)))
'List of Randomly Organized Names Only ******NEED TO SPECIFY WHERE THESE NAMES ARE COMING FROM, I used Sheet1
sh2.Activate
Set namerng = sh2.Range(Cells(1, 1), (Cells(Rows.Count, 1).End(xlUp)))
'Put correct department with name
namerng.Offset(0, 1) = Application.VLookup(namerng, Lookuprng, 2, False)
End Sub
It looks like you're missing some code to try and fulfill what you're wanting, so some of the below is an assumption.
Sub find()
Dim namerng As Range 'List of Randomly Organized Names Only
Dim Lookuprng As Range 'List of Names and Departments
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Set sh1 = ThisWorkbook.Worksheets(1) 'List of Randomly Organized Names Only
Set sh2 = ThisWorkbook.Worksheets(2) 'List of Names and Departments
'List of Names and Departments
sh1.Activate
Set Lookuprng = sh1.Range(Cells(1, 1), (Cells(Rows.Count, 2).End(xlUp)))
'List of Randomly Organized Names Only ******NEED TO SPECIFY WHERE THESE NAMES ARE COMING FROM, I used Sheet1
sh2.Activate
Set namerng = sh2.Range(Cells(1, 1), (Cells(Rows.Count, 1).End(xlUp)))
'Put correct department with name
namerng.Offset(0, 1) = Application.VLookup(namerng, Lookuprng, 2, False)
End Sub
edited Mar 26 at 21:11
answered Mar 26 at 21:06
W-hitW-hit
1888 bronze badges
1888 bronze badges
Thank you so much for the comment! This worked very well for me, with the exception of a few tweaks just because, you are right, I picked a select part from my code to post so I had a few more things going along with it I had to account for. Thank you again!!
– Jpb104
Mar 27 at 20:17
You are welcome. If my answer worked for you, please mark it as the answer by clicking the check mark.
– W-hit
Mar 27 at 20:21
Just did! Sorry still new to this site haha
– Jpb104
Mar 27 at 21:25
add a comment |
Thank you so much for the comment! This worked very well for me, with the exception of a few tweaks just because, you are right, I picked a select part from my code to post so I had a few more things going along with it I had to account for. Thank you again!!
– Jpb104
Mar 27 at 20:17
You are welcome. If my answer worked for you, please mark it as the answer by clicking the check mark.
– W-hit
Mar 27 at 20:21
Just did! Sorry still new to this site haha
– Jpb104
Mar 27 at 21:25
Thank you so much for the comment! This worked very well for me, with the exception of a few tweaks just because, you are right, I picked a select part from my code to post so I had a few more things going along with it I had to account for. Thank you again!!
– Jpb104
Mar 27 at 20:17
Thank you so much for the comment! This worked very well for me, with the exception of a few tweaks just because, you are right, I picked a select part from my code to post so I had a few more things going along with it I had to account for. Thank you again!!
– Jpb104
Mar 27 at 20:17
You are welcome. If my answer worked for you, please mark it as the answer by clicking the check mark.
– W-hit
Mar 27 at 20:21
You are welcome. If my answer worked for you, please mark it as the answer by clicking the check mark.
– W-hit
Mar 27 at 20:21
Just did! Sorry still new to this site haha
– Jpb104
Mar 27 at 21:25
Just did! Sorry still new to this site haha
– Jpb104
Mar 27 at 21:25
add a comment |
Based on your description of the issue and the problem you want to solve, I believe that the following code should work as needed.
Sub find()
Dim rng As Range
Dim depti As Integer
Dim dept As String
Set rng = Sheet1.Range("A1:B250") 'range of names in col A and dept in col B
For depti = 3 To 250
dept = "=IfError(Vlookup(A" & depti & ",'" & Sheet1.Name & "'!" & rng.Address & ",2,FALSE), """")"
Range("B" & depti) = dept
Next
End Sub
Notes
I eliminated the namerng
variable, because we don't need it for the updated equation. Since VLookup only takes a single value as the first param, that value should be dependent on the value of the loop variable, same as the destination range is.
I also escaped the quotation marks, so that the function would be valid, and added in the sheet1.name, so that the range address will point to the correct sheet (with single quotation marks, in case the sheet name has a space in it).
A cautionary note: the way this is coded, the equation will be inserted on whichever sheet is the active one at the time of the code run. This means that if Sheet1
is active, the departments will get overwritten with a circular-formula.
W-hit's answer looks like it will fill in the actually department values, while mine will fill in an excel equation which returns them. (stackoverflow.com/a/55366184)
– Mistella
Mar 26 at 21:09
Thank you so much for the response! This was getting the program to run, but would not pull the values from the second sheet that I needed. I think it was an issue on my end however, as my formulas in the sheet got messed up along the line, and anyone who sees this in the future can use this to help them out
– Jpb104
Mar 27 at 20:15
add a comment |
Based on your description of the issue and the problem you want to solve, I believe that the following code should work as needed.
Sub find()
Dim rng As Range
Dim depti As Integer
Dim dept As String
Set rng = Sheet1.Range("A1:B250") 'range of names in col A and dept in col B
For depti = 3 To 250
dept = "=IfError(Vlookup(A" & depti & ",'" & Sheet1.Name & "'!" & rng.Address & ",2,FALSE), """")"
Range("B" & depti) = dept
Next
End Sub
Notes
I eliminated the namerng
variable, because we don't need it for the updated equation. Since VLookup only takes a single value as the first param, that value should be dependent on the value of the loop variable, same as the destination range is.
I also escaped the quotation marks, so that the function would be valid, and added in the sheet1.name, so that the range address will point to the correct sheet (with single quotation marks, in case the sheet name has a space in it).
A cautionary note: the way this is coded, the equation will be inserted on whichever sheet is the active one at the time of the code run. This means that if Sheet1
is active, the departments will get overwritten with a circular-formula.
W-hit's answer looks like it will fill in the actually department values, while mine will fill in an excel equation which returns them. (stackoverflow.com/a/55366184)
– Mistella
Mar 26 at 21:09
Thank you so much for the response! This was getting the program to run, but would not pull the values from the second sheet that I needed. I think it was an issue on my end however, as my formulas in the sheet got messed up along the line, and anyone who sees this in the future can use this to help them out
– Jpb104
Mar 27 at 20:15
add a comment |
Based on your description of the issue and the problem you want to solve, I believe that the following code should work as needed.
Sub find()
Dim rng As Range
Dim depti As Integer
Dim dept As String
Set rng = Sheet1.Range("A1:B250") 'range of names in col A and dept in col B
For depti = 3 To 250
dept = "=IfError(Vlookup(A" & depti & ",'" & Sheet1.Name & "'!" & rng.Address & ",2,FALSE), """")"
Range("B" & depti) = dept
Next
End Sub
Notes
I eliminated the namerng
variable, because we don't need it for the updated equation. Since VLookup only takes a single value as the first param, that value should be dependent on the value of the loop variable, same as the destination range is.
I also escaped the quotation marks, so that the function would be valid, and added in the sheet1.name, so that the range address will point to the correct sheet (with single quotation marks, in case the sheet name has a space in it).
A cautionary note: the way this is coded, the equation will be inserted on whichever sheet is the active one at the time of the code run. This means that if Sheet1
is active, the departments will get overwritten with a circular-formula.
Based on your description of the issue and the problem you want to solve, I believe that the following code should work as needed.
Sub find()
Dim rng As Range
Dim depti As Integer
Dim dept As String
Set rng = Sheet1.Range("A1:B250") 'range of names in col A and dept in col B
For depti = 3 To 250
dept = "=IfError(Vlookup(A" & depti & ",'" & Sheet1.Name & "'!" & rng.Address & ",2,FALSE), """")"
Range("B" & depti) = dept
Next
End Sub
Notes
I eliminated the namerng
variable, because we don't need it for the updated equation. Since VLookup only takes a single value as the first param, that value should be dependent on the value of the loop variable, same as the destination range is.
I also escaped the quotation marks, so that the function would be valid, and added in the sheet1.name, so that the range address will point to the correct sheet (with single quotation marks, in case the sheet name has a space in it).
A cautionary note: the way this is coded, the equation will be inserted on whichever sheet is the active one at the time of the code run. This means that if Sheet1
is active, the departments will get overwritten with a circular-formula.
edited Mar 26 at 21:12
answered Mar 26 at 21:06
MistellaMistella
1,2512 gold badges5 silver badges17 bronze badges
1,2512 gold badges5 silver badges17 bronze badges
W-hit's answer looks like it will fill in the actually department values, while mine will fill in an excel equation which returns them. (stackoverflow.com/a/55366184)
– Mistella
Mar 26 at 21:09
Thank you so much for the response! This was getting the program to run, but would not pull the values from the second sheet that I needed. I think it was an issue on my end however, as my formulas in the sheet got messed up along the line, and anyone who sees this in the future can use this to help them out
– Jpb104
Mar 27 at 20:15
add a comment |
W-hit's answer looks like it will fill in the actually department values, while mine will fill in an excel equation which returns them. (stackoverflow.com/a/55366184)
– Mistella
Mar 26 at 21:09
Thank you so much for the response! This was getting the program to run, but would not pull the values from the second sheet that I needed. I think it was an issue on my end however, as my formulas in the sheet got messed up along the line, and anyone who sees this in the future can use this to help them out
– Jpb104
Mar 27 at 20:15
W-hit's answer looks like it will fill in the actually department values, while mine will fill in an excel equation which returns them. (stackoverflow.com/a/55366184)
– Mistella
Mar 26 at 21:09
W-hit's answer looks like it will fill in the actually department values, while mine will fill in an excel equation which returns them. (stackoverflow.com/a/55366184)
– Mistella
Mar 26 at 21:09
Thank you so much for the response! This was getting the program to run, but would not pull the values from the second sheet that I needed. I think it was an issue on my end however, as my formulas in the sheet got messed up along the line, and anyone who sees this in the future can use this to help them out
– Jpb104
Mar 27 at 20:15
Thank you so much for the response! This was getting the program to run, but would not pull the values from the second sheet that I needed. I think it was an issue on my end however, as my formulas in the sheet got messed up along the line, and anyone who sees this in the future can use this to help them out
– Jpb104
Mar 27 at 20:15
add a 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%2f55365720%2fvlookup-function-in-vba-return-a-department-value-based-on-name-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
Which line are you getting the error on?
– Mistella
Mar 26 at 20:35
Also, if you're adding a VBA reference into a string equation for Excel, you need to add the variables like
"=IfError(Vlookup(" & "namerng & "," & rng & ",2,FALSE), """")"
(Note the extra quotation marks to add them to the string. if you didDebug.Print "=IfError(Vlookup(namerng,rng,2,FALSE), "")"
, the output would be=IfError(Vlookup(namerng,rng,2,FALSE), ")
)– Mistella
Mar 26 at 20:38
I am getting the error on the Range("B" & depti) = dept line
– Jpb104
Mar 26 at 20:40
Adding that debug.print got the program to run, but no values are in column B for the department
– Jpb104
Mar 26 at 20:43
I believe if you use
dept = "=IfError(Vlookup(" & namerng.Address & "," & rng.Address & ",2,FALSE), """")"
, it should run and insert the equation. However, I'm not convinced that this equation is what you want. Right now, there are circular references (everything's happening on the same sheet), and VLOOKUP only accepts one value for the first param, not a range of multiple cells.– Mistella
Mar 26 at 20:52