Excel VLOOKUP Macro
Solved/Closed
froggy6703
Posts
16
Registration date
Tuesday March 23, 2010
Status
Member
Last seen
March 29, 2013
-
Apr 8, 2010 at 06:23 PM
froggy6703 Posts 16 Registration date Tuesday March 23, 2010 Status Member Last seen March 29, 2013 - Apr 9, 2010 at 04:24 PM
froggy6703 Posts 16 Registration date Tuesday March 23, 2010 Status Member Last seen March 29, 2013 - Apr 9, 2010 at 04:24 PM
Related:
- Excel VLOOKUP Macro
- Spell number in excel without macro - Guide
- Excel marksheet - Guide
- Excel free download - Download - Spreadsheets
- Macros in excel download free - Download - Spreadsheets
- Kernel for excel - Download - Backup and recovery
8 responses
froggy6703
Posts
16
Registration date
Tuesday March 23, 2010
Status
Member
Last seen
March 29, 2013
2
Apr 9, 2010 at 02:37 PM
Apr 9, 2010 at 02:37 PM
I tried change the code to this and now I am getting the following error...
AutoFill method of Range class failed
Any ideas how to fix this?
Also for the other part, I had look into that this morning. The problem I am seeing with that is if the user enters "AB2" as there start range. This would then result in the variables containing A(sCol) & B2(lRow). That is why I thought if I could trim the alfa or numeric values I would prevent this from happening.
AutoFill method of Range class failed
Any ideas how to fix this?
Also for the other part, I had look into that this morning. The problem I am seeing with that is if the user enters "AB2" as there start range. This would then result in the variables containing A(sCol) & B2(lRow). That is why I thought if I could trim the alfa or numeric values I would prevent this from happening.
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Apr 8, 2010 at 06:48 PM
Apr 8, 2010 at 06:48 PM
Please do explain more. I don't get what you mean by replace by "vRange + sLastRow variables." I am also not getting you where this +/- value in loop up would be coming from. I am not so clear about third part too
From what all I could understand
you can change this
ActiveCell.FormulaR1C1 = "=VLOOKUP(TRIM(RC[-1]),Lookup_Table!R1C1:R5000C2,2,0)"
to
Dim colOffset
colOffset = -1
ActiveCell.FormulaR1C1 = "=VLOOKUP(TRIM(RC[" & colOffset & "]),Lookup_Table!R1C1:R" & sLastRow & "C2,2,0)"
and this
Selection.AutoFill Destination:=Cells("A2:A" + sLastRow)
to
Selection.AutoFill Destination:=Cells("A2:A" & sLastRow)
From what all I could understand
you can change this
ActiveCell.FormulaR1C1 = "=VLOOKUP(TRIM(RC[-1]),Lookup_Table!R1C1:R5000C2,2,0)"
to
Dim colOffset
colOffset = -1
ActiveCell.FormulaR1C1 = "=VLOOKUP(TRIM(RC[" & colOffset & "]),Lookup_Table!R1C1:R" & sLastRow & "C2,2,0)"
and this
Selection.AutoFill Destination:=Cells("A2:A" + sLastRow)
to
Selection.AutoFill Destination:=Cells("A2:A" & sLastRow)
froggy6703
Posts
16
Registration date
Tuesday March 23, 2010
Status
Member
Last seen
March 29, 2013
2
Apr 8, 2010 at 06:55 PM
Apr 8, 2010 at 06:55 PM
Rizvisa1 thank you for responding so fast. You actually helped a lot with what you said above that solves two of the three questions already. The only thing I have a question on is in the statement Selection.AutoFill Destination:=Cells("A2:A" + sLastRow) I would like to replace the "A2:A" part somehow with a variable that can be captured through an input box. Do you know how that would be done.
Thanks again,
Matt
Thanks again,
Matt
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Apr 8, 2010 at 09:22 PM
Apr 8, 2010 at 09:22 PM
Matt, not knowing how A2A: may change, I am presuming that only part that will change will be the "A" part, You can have an input box as you had earlier like
vRange2 = InputBox("Enter Lookup Column")
Selection.AutoFill Destination:=Cells(vRange2 & 2 & ":" & vRange2 & sLastRow)
vRange2 = InputBox("Enter Lookup Column")
Selection.AutoFill Destination:=Cells(vRange2 & 2 & ":" & vRange2 & sLastRow)
Didn't find the answer you are looking for?
Ask a question
froggy6703
Posts
16
Registration date
Tuesday March 23, 2010
Status
Member
Last seen
March 29, 2013
2
Apr 9, 2010 at 12:05 PM
Apr 9, 2010 at 12:05 PM
I am getting an error message when I try that. But it does work for the next line that selects the range. Also I will need to change both the "A" part and the "2" part. I was thinking instead of having two input boxes to capture each of these values. Is there a way to trim off the numeric value from a valiable, or the alfebetic value of a variable. What I am thinking is taking the variable vRange (which equals B1). Now trimming it both ways creating two new variables call vRangeA (which equals B)) and vRangeN (which equals 1).
Here is the code I am using...
'Find matching item numbers on the Lookup_Table Tab
Range(vRange).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(TRIM(RC[" & colOffset & "]),Lookup_Table!R1C1:R" & sLTLastRow & "C2,2,0)"
'Getting Error Message On This Line
Selection.AutoFill Destination:=Cells(vRange2 & 2 & ":" & vRange2 + sLastRow)
'This Line Works OK Though
Range(vRange2 & 2 & ":" & vRange2 + sLastRow).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Cells.EntireColumn.AutoFit
Here is the code I am using...
'Find matching item numbers on the Lookup_Table Tab
Range(vRange).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(TRIM(RC[" & colOffset & "]),Lookup_Table!R1C1:R" & sLTLastRow & "C2,2,0)"
'Getting Error Message On This Line
Selection.AutoFill Destination:=Cells(vRange2 & 2 & ":" & vRange2 + sLastRow)
'This Line Works OK Though
Range(vRange2 & 2 & ":" & vRange2 + sLastRow).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Cells.EntireColumn.AutoFit
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Apr 9, 2010 at 02:23 PM
Apr 9, 2010 at 02:23 PM
Try this
Selection.AutoFill Destination:=Range(vRange2 & 2 & ":" & vRange2 & sLastRow)
You cannot have like CELL("A2:A10"). CELLS address is always one cell and not more. You have to use range in this case as above
About separating number and text. Yes it can be done
Dim sCol as String
Dim lRow as long
Dim vRange3 as string
vRange3 = inputbox("Enter Start Range (example A4)")
sCol =Left(Vrange3, 1) 'this gets the 1 character from left
lRow = mid(vRange3, 2) 'this gets characters starting from position number 2 till end
Selection.AutoFill Destination:=Range(vRange2 & 2 & ":" & vRange2 & sLastRow)
You cannot have like CELL("A2:A10"). CELLS address is always one cell and not more. You have to use range in this case as above
About separating number and text. Yes it can be done
Dim sCol as String
Dim lRow as long
Dim vRange3 as string
vRange3 = inputbox("Enter Start Range (example A4)")
sCol =Left(Vrange3, 1) 'this gets the 1 character from left
lRow = mid(vRange3, 2) 'this gets characters starting from position number 2 till end
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Apr 9, 2010 at 03:10 PM
Apr 9, 2010 at 03:10 PM
I dont get any error. Could you post your workbook at some share site like https://authentification.site and post link here,
For inputbox, then you can do this
Dim sCol As String
Dim sRow As String
sCol = InputBox("Enter Start Range (example A4)")
sCol = Trim(sCol)
Do While (IsNumeric(Right(sCol, 1)))
sRow = Right(sCol, 1) & sRow
sCol = Trim(Mid(sCol, 1, Len(sCol) - 1))
Loop
For inputbox, then you can do this
Dim sCol As String
Dim sRow As String
sCol = InputBox("Enter Start Range (example A4)")
sCol = Trim(sCol)
Do While (IsNumeric(Right(sCol, 1)))
sRow = Right(sCol, 1) & sRow
sCol = Trim(Mid(sCol, 1, Len(sCol) - 1))
Loop
froggy6703
Posts
16
Registration date
Tuesday March 23, 2010
Status
Member
Last seen
March 29, 2013
2
Apr 9, 2010 at 04:24 PM
Apr 9, 2010 at 04:24 PM
Hey, thank you this worked. I changed the code to accept the new code you just listed to split the variable and now everything is working great.
Thanks again for all your help.
Matt
Thanks again for all your help.
Matt