Gary Croxford
asked on
Need Help with VLookup in VBA
Thank you for looking at my question,
I am trying to execute a VLOOKUP() function in VBA but when it is transposed there are extra brackets in the statement that change it's result completely.
The idea is to compare a concatenation of cells in columns A and D with the contents of .Sheet("Stock Point Inventory") with the values in cells in column D of .Sheets("Fixed Locations")
The Excel function I am trying to replicate is
=IF(ISERROR(VLOOKUP(CONCAT ENATE(A2," -",D2),'Fi xed Locations'!D:D,1,0)),"NOK" ,"OK")
In VBA I have written the function as:
strVLookup = "=IF(ISNA(VLOOKUP(CONCATEN ATE(RC[-9] ," & strQuote & "-" & strQuote & ",RC[-6]),'Fixed Locations'!D:D,1,0))," & strQuote & "NOK" & strQuote & "," & strQuote & "OK" & strQuote & ")"
When the routine runs the text I expected to see in the formula bar is
=IF(ISNA(VLOOKUP(CONCATENA TE(A1336," -",D1336), 'Fixed Locations'!D:D,1,0)),"NOK" ,"OK")
But what I am seeing is
=IF(ISNA(VLOOKUP(CONCATENA TE(A1339," -",D1339), 'Fixed Locations'!D:(D),1,0)),"NO K","OK") - note the extra set of brackets in 'Fixed Locations'!D:(D),
Can anybody tell me please why this is happening and how to correct it?
The code I use is
I am trying to execute a VLOOKUP() function in VBA but when it is transposed there are extra brackets in the statement that change it's result completely.
The idea is to compare a concatenation of cells in columns A and D with the contents of .Sheet("Stock Point Inventory") with the values in cells in column D of .Sheets("Fixed Locations")
The Excel function I am trying to replicate is
=IF(ISERROR(VLOOKUP(CONCAT
In VBA I have written the function as:
strVLookup = "=IF(ISNA(VLOOKUP(CONCATEN
When the routine runs the text I expected to see in the formula bar is
=IF(ISNA(VLOOKUP(CONCATENA
But what I am seeing is
=IF(ISNA(VLOOKUP(CONCATENA
Can anybody tell me please why this is happening and how to correct it?
The code I use is
Sub Process_VLookup()
strQuote = Chr(34)
'' Filename
strPath_SPI = "C:\Stock Point Inventory\Stock Point Inventory.xlsx"
Set wbSPI = Application.Workbooks.Open(strPath_SPI)
'' Get No Rows in Worksheet
lngSPI_TotalRows = wbSPI.Worksheets("Stock Point Inventory").Cells(65536, "A").End(xlUp).Row
MsgBox "Total Rows: " & lngSPI_TotalRows, vbOKOnly
'' Function String
strVLookup = "=IF(ISNA(VLOOKUP(CONCATENATE(RC[-9]," & strQuote & "-" & strQuote & ",RC[-6]),'Fixed Locations'!D2:D" & lngSPI_TotalRows & ",1,0))," & strQuote & "NOK" & strQuote & "," & strQuote & "OK" & strQuote & ")"
With wbSPI.Sheets("Stock Point Inventory")
lngSPI_Row = .Range("J2").Row
lngSPI_Col = .Range("J2").Column
'' Range is all cells in Col J
Set rngRange = Range("J2:J" & lngSPI_TotalRows)
'' Apply function to each in cell in col J
For Each rngCell In rngRange
.Cells(lngSPI_Row, lngSPI_Col).FormulaR1C1 = strVLookup
lngSPI_Row = lngSPI_Row + 1
Next rngCell
'' Apply conditional formatting
.Cells.FormatConditions.Delete
With .Range("A2:J" & lngSPI_TotalRows).FormatConditions.Add(Type:=xlExpression, Formula1:="=INDIRECT(""J"" & ROW())=""NOK""")
.SetFirstPriority
.Font.Color = vbRed
.StopIfTrue = True
End With
'' Sort Data With NOK's at the top of the list
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("J2:J" & lngSPI_TotalRows), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
.Sort.SortFields.Add Key:=Range("A2:A" & lngSPI_TotalRows), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortTextAsNumbers
With wbSPI.Sheets("Stock Point Inventory").Sort
.SetRange Range("A1:J" & lngSPI_TotalRows)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
End Sub
Stock-Point-Inventory.xlsx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also, I see you are attempting to change 'Fixed Locations'!D:D to the extent of the data in column [D] (donated by the variable lngSPI_TotalRows).
---
lngSPI_TotalRows = wbSPI.Worksheets("Stock Point Inventory").Cells(65536, "A").End(xlUp).Row
strVLookup = "=IF(ISNA(VLOOKUP(CONCATEN ATE(RC[-9] ," & strQuote & "-" & strQuote & ",RC[-6]),'Fixed Locations'!D2:D" & lngSPI_TotalRows & ",1,0))," & strQuote & "NOK" & strQuote & "," & strQuote & "OK" & strQuote & ")"
---
This is the cause of your problem, I think.
I would be tempted to use this simple change:
From...
.Cells(lngSPI_Row, lngSPI_Col).FormulaR1C1 = strVLookup
To...
.Cells(lngSPI_Row, lngSPI_Col).FormulaR1C1 = Replace("=IF(ISNA(VLOOKUP( CONCATENAT E(A$1,""-" ",D$1),'Fi xed Locations'!D:D,1,0)),""NOK "",""OK"") ", "$1", rngCell.Row)
That may make the code easier to read/maintain.
If you do wish to restrict the usage of column [D], I would suggest not assuming the worksheet has 65,536 rows either.
Change:
lngSPI_TotalRows = wbSPI.Worksheets("Stock Point Inventory").Cells(65536, "A").End(xlUp).Row
To:
lngSPI_TotalRows = wbSPI.Worksheets("Stock Point Inventory").Cells(Rows.Count, "A").End(xlUp).Row
---
lngSPI_TotalRows = wbSPI.Worksheets("Stock Point Inventory").Cells(65536, "A").End(xlUp).Row
strVLookup = "=IF(ISNA(VLOOKUP(CONCATEN
---
This is the cause of your problem, I think.
I would be tempted to use this simple change:
From...
.Cells(lngSPI_Row, lngSPI_Col).FormulaR1C1 = strVLookup
To...
.Cells(lngSPI_Row, lngSPI_Col).FormulaR1C1 = Replace("=IF(ISNA(VLOOKUP(
That may make the code easier to read/maintain.
If you do wish to restrict the usage of column [D], I would suggest not assuming the worksheet has 65,536 rows either.
Change:
lngSPI_TotalRows = wbSPI.Worksheets("Stock Point Inventory").Cells(65536, "A").End(xlUp).Row
To:
lngSPI_TotalRows = wbSPI.Worksheets("Stock Point Inventory").Cells(Rows.Count, "A").End(xlUp).Row
try change:
strVLookup = "=IF(ISNA(VLOOKUP(CONCATEN ATE(RC[-9] ," & strQuote & "-" & strQuote & ",RC[-6]),'Fixed Locations'!D2:D" & lngSPI_TotalRows & ",1,0))," & strQuote & "NOK" & strQuote & "," & strQuote & "OK" & strQuote & ")"
to:
strVLookup = "=IF(ISNA(VLOOKUP(CONCATEN ATE(RC[-9] ,""-"",RC[ -6]),'Fixe d Locations'!C[-6],1,0)),""N OK"",""OK" ")"
strVLookup = "=IF(ISNA(VLOOKUP(CONCATEN
to:
strVLookup = "=IF(ISNA(VLOOKUP(CONCATEN
Hi,
instead of
instead of
strVLookup = "=IF(ISNA(VLOOKUP(CONCATENATE(RC[-9]," & strQuote & "-" & strQuote & ",RC[-6]),'Fixed Locations'!D2:D" & lngSPI_TotalRows & ",1,0))," & strQuote & "NOK" & strQuote & "," & strQuote & "OK" & strQuote & ")"
With wbSPI.Sheets("Stock Point Inventory")
lngSPI_Row = .Range("J2").Row
lngSPI_Col = .Range("J2").Column
'' Range is all cells in Col J
Set rngRange = Range("J2:J" & lngSPI_TotalRows)
'' Apply function to each in cell in col J
For Each rngCell In rngRange
.Cells(lngSPI_Row, lngSPI_Col).FormulaR1C1 = strVLookup
lngSPI_Row = lngSPI_Row + 1
Next rngCell
You could use strVLookup = "=IF(ISNA(VLOOKUP(CONCATENATE(RC[-9]," & strQuote & "-" & strQuote & ",RC[-6]),'Fixed Locations'!C[-6]" & lngSPI_TotalRows & ",1,0))," & strQuote & "NOK" & strQuote & "," & strQuote & "OK" & strQuote & ")"
With Sheets("Stock Point Inventory")
lngSPI_Row = .Range("J2").Row
lngSPI_Col = .Range("J2").Column
Range("J2").FormulaR1C1 = strVLookup
'' Range is all cells in Col J
Set rngRange = Range("J2:J" & lngSPI_TotalRows)
Range("J2").AutoFill rngRange
ASKER
RGonzo1971 Thank you, works a treat
You're welcome.
Oh.
Oh.
When the routine runs the text I expected to see in the formula bar is
=IF(ISNA(VLOOKUP(CONCATENA
But what I am seeing is
=IF(ISNA(VLOOKUP(CONCATENA
--
Are the row numbers changing too, or was that just a copy'n'paste issue with your question text?