Home » excel » Excel VBA Match function to take either String or Integer variables as Arg1

Excel VBA Match function to take either String or Integer variables as Arg1

Posted by: admin May 14, 2020 Leave a comment

Questions:

The following is part of a longer code.
How can I declare Arg1 in Application.WorksheetFunction.Match to be either an Integer or a String?

I have data (numbers) in column A and numbers with underscores in column B.

The user would enter either a number or a string in an inputbox and the match function will try to match the user input in column A but if it not matched, then it would try to match it in column B and return the match row.

However, the matching will only work either if I use Dim userinput As Integer and that would be able to match in column A only; or use Dim userinput As String and that would be able to match it in column B only. Declaring Dim userinput As Variant would work just similar to Dim userinput As String.

I could do some workaround but I would like to understand first why it is not able to match it in column A when declaring it as Variant?

Following is the data I have in columns A and B, respectively.

W   WB
25  25_0
25  25_1
25  25_2
25  25_2
25  25_3
25  25_3
25  25_3
26  26_0
26  26_0
26  26_0
26  26_1
26  26_1

Following is the code:

Sub Return_Row()
Dim userinput As Variant, search_rng As Range

userinput = InputBox("Enter your number")

Set search_rng = ActiveSheet.Range("A:A")

On Error Resume Next
N = Application.WorksheetFunction.Match(userinput, search_rng, 0)
On Error GoTo 0

If N = 0 Then
    Set search_rng = ActiveSheet.Range("B:B")
    N = Application.WorksheetFunction.Match(userinput, search_rng, 0)
End If

MsgBox N
End Sub
How to&Answers:

Thanks Scott Craner, it worked!. It is my first time to learn about the CDec function.

This way (using the Val function) is also working.

Sub Return_Row()
Dim search_rng As Range

userinput = InputBox("Enter your number")

On Error Resume Next
If IsNumeric(userinput) Then
    userinput = Val(userinput)
    Set search_rng = ActiveSheet.Range("A:A")
    N = Application.WorksheetFunction.Match(userinput, search_rng, 0)
Else
    Set search_rng = ActiveSheet.Range("B:B")
    N = Application.WorksheetFunction.Match(userinput, search_rng, 0)
End If

On Error GoTo 0


MsgBox N
End Sub

Answer:

Here’s another approach:

Sub foo()
    Dim userinput
    userinput = Application.InputBox("your value", Type:=3)

Select Case TypeName(userinput)
    Case "Double"
        'do the numeric match
    Case "String"
        'do the string match
    Case Boolean
        'Cancel button routine
End Select
End Sub

Application.InputBox unlike InputBox (which is really VBA.InputBox), allows you to specify the return types.

As @MathieuGuindon points out, there are some benefits to using VarType instead of TypeName. Below is code using the latter:

Option Explicit
Sub foo()
    Dim userinput
    userinput = Application.InputBouserinput("your value", Type:=3)

Select Case VarType(userinput)
    Case vbDouble
        'do the numeric match
    Case vbString
        'do the string match
    Case vbBoolean
        'Cancel button was pressed
        Stop
End Select
End Sub