VBA - Index Match Functions - when calling getting "ByRef argument type mismatch"

kpaull

New Member
Joined
Apr 18, 2008
Messages
20
When running this code below the VBE highlights ImportSheetRange in this line: "ReturnValue = Indexmatch(strSKUEntered, ImportSheetRange, ExportSheetRange)" and spits the error "ByRef argument type mismatch".

Any help.

Running Excel 365 / Window 7.

Thanks!


Code:
Option Explicit
Sub Macro1()
    
    Dim kpResults, datExportDate, strImportDate1, strImportDate2, strSKUEntered As String
    Dim ImportSheetRange, ExportSheetRange As Range
    Dim ReturnValue As Variant
    
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
        .DisplayAlerts = False
    End With


'Prompt for SKU - provide default value as a guide/mask and allow to click on SKU in spreadsheet instead of typing in value


        strSKUEntered = Application.InputBox("Enter a SKU to process", , "242738-797-REM", , , , Type:=2)
        
'Find SKU on Export Worksheet, write -3 columns cell value (Date) to a variable
'Worksheet function formula in SS = =INDEX(Import!$A$2:$O$250000,SMALL(IF(Import!$A:$A=$D$452,ROW($A:$A)),ROW(1:1))-1,15)
  
        Set ImportSheetRange = ActiveWorkbook.Sheets("Import").Range("A2:P100000")
        Set ExportSheetRange = ActiveWorkbook.Sheets("Export").Range("A2:E100000")
  
        ReturnValue = Indexmatch(strSKUEntered, ImportSheetRange, ExportSheetRange)
               
        Debug.Print ReturnValue
          
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        .DisplayAlerts = True
    End With
    
    
End Sub




Function Indexmatch(LookFor As String, SearchRng As Range, ReturnRng As Range) As Variant
    
    Application.Volatile
    
    Indexmatch = WorksheetFunction.Index(ReturnRng, WorksheetFunction.Match(LookFor, SearchRng, 0), 0)
    
End Function
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
When declaring multiple variables on a single line on the last one will have the stated type all others will be type variant unless explicated declared

So importsheetrange will be variant not range

Same with your string variables
 
Last edited:
Upvote 0
The second parameter of Match, lookup_array (SearchRng in the Indexmatch function), must be a one-dimensional range or array. It seems the code is calling the function using a two-dimensional range, i.e., ImportSheetRange (A2:P100000)

Also the last parameter, 0, doesn't seem correct - in this case i think you should use a column number

M.
 
Upvote 0
The second parameter of Match, lookup_array (SearchRng in the Indexmatch function), must be a one-dimensional range or array. It seems the code is calling the function using a two-dimensional range, i.e., ImportSheetRange (A2:P100000)

Also, the last parameter, 0, doesn't seem correct - in this case i think you should use a column number

M.

Thank you, Marcelo! I made the adjustments you suggested in my code below, marked in Red text. Still get the same error on arrImportSheet array.

Rich (BB code):
Option Explicit
Sub Macro1()
    
    Dim kpResults, datExportDate, strImportDate1, strImportDate2, strSKUEntered As String
    Dim ExportSheetRange As Range
    Dim arrImportSheet(10) As String
    Dim ReturnValue As Variant
    
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
        .DisplayAlerts = False
    End With


'Prompt for SKU - provide default value as a guide/mask and allow to click on SKU in spreadsheet instead of typing in value


        strSKUEntered = Application.InputBox("Enter a SKU to process", , "242738-797-REM", , , , Type:=2)
        
'Find SKU on Export Worksheet, write -3 columns cell value (Date) to a variable
'Worksheet function formula in SS = =INDEX(Import!$A$2:$O$250000,SMALL(IF(Import!$A:$A=$D$452,ROW($A:$A)),ROW(1:1))-1,15)
  
            
        Set ExportSheetRange = ActiveWorkbook.Sheets("Export").Range("A2:E100000")
  
        ReturnValue = Indexmatch(strSKUEntered, arrImportSheet, ExportSheetRange)
               
        Debug.Print ReturnValue
          
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        .DisplayAlerts = True
    End With
    
    
End Sub


Function Indexmatch(LookFor As String, SearchRng As Range, ReturnRng As Range) As Variant
    
    Application.Volatile
    
    Indexmatch = WorksheetFunction.Index(ReturnRng, WorksheetFunction.Match(LookFor, SearchRng, 1), 0)
      
End Function
 
Upvote 0
Thank you for your quick reply jimrward!

As I understand it "Function Indexmatch(LookFor As String, SearchRng As Range, ReturnRng As Range) As Variant" means...

The return value will be in Indexmatcxh and it will be a Variant.

The variables mentioned are as mentioned.

I could be mistaken.
 
Upvote 0
When declaring multiple variables on a single line on the last one will have the stated type all others will be type variant unless explicated declared

So importsheetrange will be variant not range

Same with your string variables
Thank you for your quick reply jimrward!

As I understand it "Function Indexmatch(LookFor As String, SearchRng As Range, ReturnRng As Range) As Variant" means...

The return value will be in Indexmatcxh and it will be a Variant.

The variables mentioned are as mentioned.

I could be mistaken.

 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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