Problem with variable VLOOKUP value

Millerj64

New Member
Joined
Jan 25, 2013
Messages
3
First off thanks for the help

I'm trying to use a variable for the VLOOKUP value. The below code works however it adds parenthesis around the VLOOKUP value, the result is it's not being recognized as a cell location. I have tried changing the variable types but have not had any luck.
This is an example of what gets put into the cell
"=VLOOKUP('a4','prod a'!$A$1:$C$800,2,FALSE)"

Need help in removing the parenthesis around the lookup value

Below is the code,
Dim i As Integer
Dim name As String

'User will select column to update via input box

Select Case ActiveCell.Value

Case "PROD A"
ActiveCell.Offset(1, 0).range("a1").Select

FinalRow = Cells(ROWs.count, 1).End(xlUp).row
For i = 2 To FinalRow

' to check that i changes (checked out and then commented out)
' MsgBox i

' add i to column A to get cell location for vlookup
name = "a" & i
' to check that cell location is correct (checked and commented out)
' MsgBox name

' two methods of naming vlookup value both result in parenthesis being added
ActiveCell.FormulaR1C1 = "=VLOOKUP(" & name & " ,'prod a'!R1C1:R800C3,2,FALSE)"
' ActiveCell.FormulaR1C1 = "=VLOOKUP(" & "a" & i & ",'prod a'!R1C1:R800C3,2,FALSE)"

ActiveCell.Offset(1, 0).range("A1").Select
Next i

' Case "PROD 20" Same code as above
' Case "PROD 21" ""
' Case "PROD 21" ""
' Case "PROD 21" ""

End Select

Thanks again
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
instead of a for loop use for each as in
Code:
Dim rng As Range
 Dim name As String

 'User will select column to update via input box

 Select Case ActiveCell.Value

 Case "PROD A"
 ActiveCell.Offset(1, 0).Range("a1").Select

 FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
 For Each rng In Range("A2:A" & FinalRow)


 ActiveCell.FormulaR1C1 = "=VLOOKUP(" & rng.Value & " ,'prod a'!R1C1:R800C3,2,FALSE)"
 ' ActiveCell.FormulaR1C1 = "=VLOOKUP(" & "a" & i & ",'prod a'!R1C1:R800C3,2,FALSE)"

 ActiveCell.Offset(1, 0).Range("A1").Select
 Next rng

 ' Case "PROD 20" Same code as above
 ' Case "PROD 21" ""
 ' Case "PROD 21" ""
 ' Case "PROD 21" ""

 End Select
 
Last edited:
Upvote 0
BarryL

Thanks for the quick turn around

I used your suggestion but now get a "Error 1004 - Application-defined or object defined error"

The for each works great, the vlookup doesn't like the rng.value

I'm using office 2010, could that be the problem?
 
Upvote 0
I don't think so

ActiveCell.Value = WorksheetFunction.VLookup(rng.Value, Range("A2:B" & FinalRow), 2, 0)

change the range as needed.
 
Upvote 0
There are a few things in your code that look a little odd to me, but at least you shouldn't need to cycle through the rows one at a time.
Try something like this in a copy of your workbook.

Code:
Dim finalrow As Long

'User will select column to update via input box

finalrow = Cells(Rows.Count, 1).End(xlUp).Row
Range(ActiveCell.Offset(1), Cells(finalrow, ActiveCell.Column)).FormulaR1C1 = "=VLOOKUP(RC1,'" & ActiveCell.Value & "'!R1C1:R800C3,2,FALSE)"
 
Last edited:
Upvote 0
Once again MrExcel SMEs assist the VBA challenged

I got it to work both ways

Thanks for all the help and appreciate your willingness to share your knowledge

Have a great weekend
 
Upvote 0
You are very welcome. Thanks for the follow-up.
 
Upvote 0

Forum statistics

Threads
1,215,676
Messages
6,126,156
Members
449,295
Latest member
DSBerry

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