ByRef argument type mismatch

nanofied

New Member
Joined
Aug 28, 2018
Messages
18
Hi guys, I am writing a code to check if a user input sheet name exist in the workbook. Here is what I done:
Code:
Function SheetExists(SheetName As String)
    On Error GoTo no:
    WorksheetName = Worksheets(SheetName).Name
    SheetExists = True
    Exit Function
no:
    SheetExists = False
End Function

Dim ImportingSheet As String

ImportingSheet = InputBox("Type in the sheet you want to import from (Case sensitive)", "Type Sheet Name", "Email 2018")
    
    If SheetExists(ImportingSheet) Then
        MsgBox "The sheet exist"
    Else
        MsgBox "The sheet does not exist. Check again"
        End
    End If

However I am getting"ByRef argument type mismatch" at the if statement. Any idea what when wrong?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
hi,

try passing the argument ByVal and see if this solves the issue.

Rich (BB code):
Function SheetExists(ByVal SheetName As String)
    On Error GoTo no:
    WorksheetName = Worksheets(SheetName).Name
    SheetExists = True
    Exit Function
no:
    SheetExists = False
End Function

Dave
 
Upvote 0
Hi, I have pass the argument as variant. And it work too. Is there any difference between the two?
 
Upvote 0
Hi, I have pass the argument as variant. And it work too. Is there any difference between the two?

The Variant data type can be used for all data types except fixed length strings and user defined types.
A Variant data type automatically works out which data type is the most appropriate based on the value you assign. It can also contain the special values "Empty", "Null" and "Error"

Whilst you can use a Variant in place of any other specified data types to work with data in a more flexible way, it’s not the most efficient way of managing your variables. If you know the data type you are dealing with it’s always best to declare it.

As an aside, you can shorten your function a little

Code:
Function SheetExists(ByVal SheetName As String) As Boolean
    On Error Resume Next
    SheetExists = CBool(Len(Worksheets(SheetName).Name) > 0)
    On Error GoTo 0
End Function

Dave
 
Upvote 0
As an aside, you can shorten your function a little
Rich (BB code):
Function SheetExists(ByVal SheetName As String) As Boolean
    On Error Resume Next
    SheetExists = CBool(Len(Worksheets(SheetName).Name) > 0)
    On Error GoTo 0
End Function
Dave
Actually, since the function is declared as a Boolean and the length of a string can never be minus one, you can shorten the red highlighted line of code to this and the function will still work correctly...

SheetExists = Len(Worksheets(SheetName).Name)
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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