Getting error: ByRef argument type mismatch - When running Function

jmazorra

Well-known Member
Joined
Mar 19, 2011
Messages
715
Hello:

I wrote the following function

Code:
Public Function sorting(Sheets As Worksheet, Column As Range, Range As Range, SortOrder As XlSortOrder)

'This function is called when a sheet(s) needs to be sorted. Two procedures
'call on this function to sort.


'@Parameter Sheets to declare sheet(s) name
'@Parameter Column to set the column to sort
'@Parameter Range to set the range to sort
'@Paramenter SortOrder to determine ascending or descending


    Application.ScreenUpdating = False
    
    With Sheets
    
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Column, SortOn:=xlSortOnValues, Order:=SortOrder, DataOption:=xlSortNormal


        With .Sort


            .SetRange Range
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply


        End With


    End With
    
    Application.ScreenUpdating = True


End Function

Now I am trying to run the following sub:

Code:
Private Sub cmbCareerPath_Change()





Application.ScreenUpdating = True


    Set WS = Worksheets("FindPath")
    Set Column = WS.Range("D:D")
    Set Rng = WS.Range("A:Z")
    
        Call sorting(WS, Column, Rng, xlAscending)


                               
            Application.ScreenUpdating = False




End Sub

However, when I run my Sub I get the compile error:
Code:
ByRef argument type mismatch
on
Code:
Call sorting(WS, Column, Rng, xlAscending)
. I am guessing is on WS, but I can't figure out why there would be a type mismatch.

Any ideas?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
However, when I run my Sub I get the compile error:
Code:
ByRef argument type mismatch
on
Code:
Call sorting(WS, Column, Rng, xlAscending)
. I am guessing is on WS, but I can't figure out why there would be a type mismatch.

Any ideas?
Just a guess... you named your first argument "Sheets"... "Sheets" is then name of a built in property of the Application object and defaults to Application when that object is not specified. Your "With Sheets" may be referencing the Application objects "Sheets" property and not the worksheet object you are passing into the function. My suggestion would be to try changing that argument name to something like Sht or WS instead.
 
Upvote 0
Hi,
Since you haven't declared variables in your calling routine, they are all variants. They probably need to be explicitly declared:

dim WS as Worksheet
dim Col as Range
dim Rng as Range
....


Note that I personally would not declare any variable names that have inbuilt object names. It's confusing and possibly buggy.
So I'd prefer define your function:
sorting(sh As Worksheet, col As Range, rng As Range, sort_order As XlSortOrder)
Or something similar i.e., with the variable names not overlapping Excel object names.
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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