Still Run time error 1004 'Unable to get the VLookup property of the WorksheetFunction class - excel

I got this error and I tried to fixed with many solution, but I don't know why this error is happening. Could you help me to fix this problem? In this code I have twice used VLookup function same source, different only Worksheet name and column. Another one can compile this code and don't have any error.
I'm doing, VLookup item on column B of worksheet "ImportData2" match with column A of worksheet "Noallocate" if match show result of VLookup at column Q of worksheet "ImportData2"
get this error
Source code:
'Vlook up function no import
Dim Vrow1 As Long
Dim myLookupValue1 As String
Dim myFirstColumn1 As Long
Dim myLastColumn1 As Long
Dim myColumnIndex1 As Long
Dim myFirstRow1 As Long
Dim myLastRow1 As Long
Dim myVLookupResult1 As String
Dim myTableArray1 As Range
For Vrow1 = 2 To 99999
myLookupValue1 = Workbooks("ExpenseData.xlsm").Worksheets("ImportData2").Range("B" & Vrow).Value
myFirstColumn1 = 1
myLastColumn1 = 2
myColumnIndex1 = 2
myFirstRow1 = 2
myLastRow1 = Workbooks("ExpenseData.xlsm").Worksheets("Noallocate").Range("b1").End(xlDown).Row
With Workbooks("ExpenseData.xlsm").Worksheets("Noallocate")
Set myTableArray1 = .Range(.Cells(myFirstRow1, myFirstColumn1), .Cells(myLastRow1, myLastColumn1))
End With
myVLookupResult1 = WorksheetFunction.VLookup(myLookupValue1, myTableArray1, myColumnIndex1, False) 'xxx
Workbooks("ExpenseData.xlsm").Worksheets("ImportData").Range("Q" & Vrow).Value = myVLookupResult1 'xxx
Next 'end function no import

Try the next code, please. It would be a good habit to have 'Option Explicit' on top of your module. This will oblige you to declare all variables. It looks, your code iterates using Vrow1 but inside the loop you used Vrow... I discovered it only after trying to make your code a little friendlier. It is not good to use big variables name instead of 1, 2. This can only make the code more difficult to be understood by looking at it. As short it would be, as easy to be understood and debugged:
Sub testVlookup()
Dim Wb As Workbook, Vrow1 As Long, lastRDat As Long, wsNoall As Worksheet
Dim wsImpD2 As Worksheet, myLookupValue1 As String, myLastRow1 As Long
Dim myVLookupResult1 As String, myTableArray1 As Range
Set Wb = Workbooks("ExpenseDataMcframe.xlsm")
Set wsNoall = Wb.Worksheets("Noallocate")
Set wsImpD2 = Wb.Worksheets("ImportData2")
myLastRow1 = wsNoall.Range("b" & Rows.count).End(xlUp).Row
lastRDat = wsImpD2.Range("B" & Rows.count).End(xlUp).Row
For Vrow1 = 2 To lastRDat
myLookupValue1 = wsImpD2.Range("B" & Vrow1).value
Set myTableArray1 = wsNoall.Range(wsNoall.Cells(2, 1), wsNoall.Cells(myLastRow1, 2))
On Error Resume Next 'for the case when lookup_value is not found
myVLookupResult1 = WorksheetFunction.VLookup(myLookupValue1, myTableArray1, 2, False) 'xxx
If Err.Number <> 0 Then
Err.Clear: On Error GoTo 0
Wb.Worksheets("ImportData").Range("Q" & Vrow1).value = "Not a mach"
Else
Wb.Worksheets("ImportData").Range("Q" & Vrow1).value = myVLookupResult1 'xxx
End If
On Error GoTo 0
Next
End Sub
When the lookup_value is not found, the code will return (on Q:Q column) "Not a match" and you must check the specific code/whatever it is...

Related

VBA code fails as soon as XML document is loaded

I have the below code which takes an XML file from a shared location and loads it into Excel. As soon as the file is opened, i get a "Run-time error '1004': Application-defined or object defined error" message.
Sub Load_XML()
Dim xml_file_path As String
Dim file_date As String
Worksheets("Start").Activate
file_date = Range("B1").Value
xml_file_path = "Y:\mydrive\" & file_date & "-000000_RREP1002.XML"
Workbooks.OpenXML Filename:= _
xml_file_path _
, LoadOption:=xlXmlLoadImportToList
Dim lstrow as Integer
Dim r as Range
lstrow = ActiveSheet.UsedRange.Rows.Count
Set r = Range("A2:AF & lstrow")
The code errors before the last line is executed, i.e. as soon as the OpenXML is done.
Any ideas please what could be causing this issue?
thanks!
I would refactor the code like below.
Public Sub Load_XML()
Dim wsStart As Worksheet
Set wsStart = ThisWorkbook.Worksheets("Start")
Dim file_date As String
file_date = wsStart.Range("B1").Value ' make sure all ranges have a worksheet specified!
Dim xml_file_path As String
xml_file_path = "Y:\mydrive\" & file_date & "-000000_RREP1002.XML"
Workbooks.OpenXML Filename:=xml_file_path, LoadOption:=xlXmlLoadImportToList
Dim lstRow As Long
lstRow = wsStart.UsedRange.Rows.Count
' I recommend a more reliable method to find the
Dim r As Range
Set r = wsStart.Range("A2:AF" & lstRow)
I recommend a more reliable method to find the last usted row than using
lstRow = wsStart.UsedRange.Rows.Count
It is better you use
lstRow = wsStart.Cells(wsStart.Rows.Count, "A").End(xlUp).Row
This will find the last used row in column A. Adjust the column to one that has always data and no blanks.

converting Excel array formula to VBA code

Hello Everyone
I've got this array formula in excel and I want to merge it in a VBA code but it gives me an error of Unable to set the FormulaArray property of the Range class (Error 1004)
Here is the formula after i ammended it in the code
"=IF(SUMPRODUCT(--(($D$2=$B9)*(E$7>=$D$3)*(E$7<=$D$4)*(NOT(E$6>=1))*(NOT(E$8='الجمعة'))))=1;VLOOKUP($D$5;LeavesTypes;2;0);"""")"
and here is the full code
Option Explicit
Option Base 1
Dim wrk As Workbook, DataSH As Worksheet, CurrentSH As Worksheet
Dim FirstCol As Integer, EmpRow As Integer, EmpID As String
Dim GetEmpRowLoop As Integer, StartingEmpCol As Integer
Dim StartColLoop As Integer, EndingEmpCol As Integer
Dim EndColLoop As Integer
Public Sub Macro1()
Set wrk = ThisWorkbook
Set DataSH = wrk.Worksheets("الاجازات ")
Set CurrentSH = wrk.ActiveSheet
If CurrentSH.Name = DataSH.Name Then
MsgBox "Please Navigate to an Active Sheet", vbCritical
Exit Sub
End If
EmpID = CurrentSH.Range("d2").Value
For GetEmpRowLoop = 9 To CurrentSH.Cells(Cells.Rows.Count, 2).End(xlUp).Row
If CurrentSH.Cells(GetEmpRowLoop, 2).Value = EmpID Then
EmpRow = GetEmpRowLoop
Exit For
End If
Next GetEmpRowLoop
For StartColLoop = 4 To CurrentSH.Cells(EmpRow, Cells.Columns.Count).End(xlToRight).Column
If CurrentSH.Cells(7, StartColLoop).Value = CurrentSH.Range("D3").Value Then
StartingEmpCol = StartColLoop
Exit For
End If
Next StartColLoop
For EndColLoop = 4 To CurrentSH.Cells(EmpRow, Cells.Columns.Count).End(xlToRight).Column
If CurrentSH.Cells(7, EndColLoop).Value = CurrentSH.Range("D4").Value Then
EndingEmpCol = EndColLoop
Exit For
End If
Next EndColLoop
CurrentSH.Range(Cells(EmpRow, StartingEmpCol), Cells(EmpRow, StartingEmpCol)).FormulaArray = _
"=IF(SUMPRODUCT(--(($D$2=$B9)*(E$7>=$D$3)*(E$7<=$D$4)*(NOT(E$6>=1))*(NOT(E$8='الجمعة'))))=1;VLOOKUP($D$5;LeavesTypes;2;0);"""")"
End Sub
I'm trying to place whether the formula itself or the result doesn't matter.
You'll need to replace the single quotes (') with double quotes (""). However, you'll need to double up on the quotes...
(NOT(E$8=""الجمعة""))
By the way, there's no need for SUMPRODUCT. The following should suffice...
.Formula = "=IF(($D$2=$B9)*(E$7>=$D$3)*(E$7<=$D$4)*(NOT(E$6>=1))*(NOT(E$8=""الجمعة""))=1;VLOOKUP($D$5;LeavesTypes;2;0);"""")"

Is there a way to reassign a Range variable to a different range?

I am very new to VBA, having started programming it yesterday. I am writing a data processing program which requires keeping track of two cells, one on each spreadsheet. The code which reproduces the errors I am experiencing is below. When I call the sub moveCell() in sub Processor(), nothing happens to DIRow and DIColumn, and the code spits out error 1004 at the line indicated. I have tried using DICell = DICell.Offset(), but it returns the same error.
How can I redefine a Range variable to be a different cell?
'<<Main Processor Code>>'
Sub Processor()
Dim PDRow As Integer
Dim PDColumn As Integer
Dim DIRow As Integer
Dim DIColumn As Integer
PDRow = 1
PDColumn = 1
DIRow = 1
DIColumn = 1
Dim PDCell As Range
Dim DICell As Range
Set PDCell = Worksheets("Processed Data").Cells(PDRow, PDColumn)
Set DICell = Worksheets("Data Input").Cells(DIRow, DIColumn)
Call moveCell(2, 0, "Data Input")
End Sub
'<<Function which moves the cell which defines the range>>'
Sub moveCell(r As Integer, c As Integer, sheet As String)
If sheet = "Processed Data" Then
PDRow = PDRow + r
PDColumn = PDColumn + c
Set PDCell = Worksheets("Data Input").Cells(PDRow, PDColumn)
ElseIf sheet = "Data Input" Then
DIRow = DIRow + r '<<<<<<This line does nothing to DIRow's value
DIColumn = DIColumn + c
Set DICell = Worksheets("Data Input").Cells(DIRow, DIColumn) '<<<<<<This line causes error 1004
End If
End Sub
As far as I can tell, you could instead use a quick Function instead. There doesn't seem to be any difference in your If statement results in the moveCell() function, except which worksheet you're using.
We can make this simpler by referring to the Range you're passing to moveCell.
Option Explicit ' forces you to declare all variables
Sub something()
Dim PDCell As Range
Set PDCell = Worksheets("Processed Data").Cells(1, 1)
Dim DICell As Range
Set DICell = Worksheets("Data Input").Cells(1, 1)
PDCell.Select ' can remove
Set PDCell = moveCell(2, 0, PDCell, PDCell.Worksheet.Name)
PDCell.Select ' can remove
Worksheets(DICell.Worksheet.Name).Activate ' can remove
DICell.Select ' can remove
Set DICell = moveCell(5, 0, DICell, DICell.Worksheet.Name)
DICell.Select ' can remove
End Sub
Function moveCell(rowsToMove As Long, colsToMove As Long, cel As Range, ws As String) As Range
Set moveCell = Worksheets(ws).Cells(cel.Row + rowsToMove, cel.Column + colsToMove)
End Function
I've included some rows you don't need (which I've marked with a comment afterwards), but that will show you how the routine works. You can step through with F8 to help see it step-by-step.
Edit: Although, you don't need a separate function at all. Just use OFFSET().
Set PDCell = ...whatever originally
Set PDCell = PDCell.Offset([rows],[cols])

Does Excel VBA WorksheetFunction need to use a select to be on the proper worksheet

I have the following code, but I have to select the worksheet otherwise the WorksheetFunction will return an error:
Function Find_Allocation_Acct_Row_Number(allocation_acct As String, allocation_cc As String)
Dim WS As Worksheet
Dim CC_row As Integer
Dim search_rng As Range
Set WS = Sheets(allocation_cc)
WS.Select
Set search_rng = WS.Range("E1:E2000")
CC_row = WS.Application.WorksheetFunction.Match(allocation_acct,search_rng, 0)
Find_Allocation_Acct_Row_Number = CC_row + 4
End Function
How can I select the right range on the right worksheet without using the Select command?
To be clear, I will ALWAYS find the value. I just need to return the row so I can create a range from it.
I have tried a number of different ways to change the "search_rng" to different variables or expressions of Range but no luck.
The error I am getting is:
Run-time error '1004':
Unable to get the Match property of the WorksheetFunction class
If the match is not found it will kick out an error and stop the code. If we late bind the match and send the result to a variant we can test for the error:
Function Find_Allocation_Acct_Row_Number(allocation_acct As String, allocation_cc As String) As Long
Dim WS As Worksheet
Set WS = Worksheets(allocation_cc)
Dim search_rng As Range
Set search_rng = WS.Range("E1:E2000")
Dim CC_row As Variant
CC_row = Application.Match(allocation_acct, search_rng, 0)
If Not IsError(CC_row) Then
Find_Allocation_Acct_Row_Number = CC_row + 4
Else
Find_Allocation_Acct_Row_Number = -1
End If
End Function
Now if a match cannot be found it will return -1

VBA Subscript out of range, name resolution problem?

Trying to write a VBA function that will return the column number given the header cell string and the worksheet name but I get the Subscript out of range error.
Here is the function:
Public Function namedColumnNo(heading As String, shtName As String) As Long
' Return the column number with named header text'
' on given worksheet.
Dim r As Range
Dim wks As Worksheet
Debug.Print shtName
'Exit Function
Set wks = Sheets(shtName)
wks.Range("1:1").Select
With wks
r = .Range("1:1").Find(heading, LookIn:=xlValue)
If r Is Nothing Then
namedColumnNo = -1
Else: namedColumnNo = r.Column
End If
End With
End Function
I am using this test sub to call the funtion:
Public Sub getCol()
Debug.Print "Find MidTemp on " & DataSht.RawDataSht
Debug.Print "Col " & namedColumnNo("MidTemp", DataSht.RawDataSht)
End Sub
I have a user defined type DataSht where I have variables to name worksheets e.g.
Public Type dataShtNames
HeaderSht As String
RawDataSht As String
ResultDataSht As String
End Type
Public DataSht As dataShtNames
With the Exit Function statement uncommented the variables resolve OK with the debug.print statements I get
Find MidTemp on RawData
RawData:MidTemp
Col 0
Leaving the function to run through the error occurs at
Set wks = Sheets(shtName)
If I replace the argument shtName with the actual sheet name as a string "RawData", then the error moves down to the line using the second argument heading. If I substitute a the parameter with a string here the error persists.
Is there something I am missing here? Some help will be much appreciated.
Sadly can't comment, but you're actually getting the out of range error because it should be LookIn:=xlValues where you have LookIn:=xlValue
As #Mathieu indicates, you'll need to fix add Set r = Find(heading, LookIn:=xlValues) to set the range to the value returned.
As a side note-you should drop the selection. Its not doing anything for you.
With wks.Range("1:1")
Set r = .Find(heading, LookIn:=xlValues)
If r Is Nothing Then
namedColumnNo = -1
Else: namedColumnNo = r.Column
End If
End With

Resources