Strange issue with range variable substitution

RIchavalues

New Member
Joined
Feb 24, 2016
Messages
25
Office Version
  1. 2019
Platform
  1. Windows
Hi All,


I tried my best to fix the issue myself and couldn't figure out for hours and hope I get some help here.


Am trying to pass a variable to a range through another subroutine. If argument is of same datatype, it works. But if multiple datatype, then getting "Type mismatch error 13". Am posting both methods below:


Method1: Below method works fine
Code:
Sub GoMainFunction(var1 As String)


ThisWorkbook.Worksheets("TestData").Select


    With ThisWorkbook.Worksheets("TestData")
        Range(var1).Select
    End With


End Sub




Sub SelectRange()


    Call GoMainFunction("A1")
    
    Call GoMainFunction("A5")


End Sub



Method2:
Code:
Function MainFunction(Fruits As Long, var1 As String) As Variant


ThisWorkbook.Worksheets("TestData").Select


Select Case Fruits
    Case "APPLE"
        With ThisWorkbook.Worksheets("TestData")
            Range(var1).Select
        End With
    
    Case "ORANGE"
        With ThisWorkbook.Worksheets("TestData")
            Range(var1).Select
        End With
    End Select
    
End Function




Sub SelectRange()


    Call MainFunction("APPLE", "A1")
    
    Call MainFunction("ORANGE", "A5")


End Sub



I have fruits as an example but in real time I have larger value and hence choosing Long datatype.


I really appreciate any help provided.


Thanks,
Richa
 
Last edited:

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.
Hi Richa,

In your MainFunction UDF you have defined Fruits as long so the function is expecting a number ranging in value from -2,147,483,648 to 2,147,483,647 but you are passing a string to it i.e. "APPLE" so Excel returns the "Type mismatch error 13" error message.

Have a look at this great web page on variables by the late Dave Hawley.

Regards,

Robert
 
Upvote 0
Thank you so much for the help Robert. I really appreciate it and the code working fine now by changing Long to String. All these days I was thinking Long data type accepts strings, my bad.

Thanks for pointing me in right direction and also for the reference web page. It's great website, hope I found it earlier. Am now changing all my code to implement this best practice and also naming convention.

Regards,
Richa
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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