0

When I run this, it gives me an error saying

ByRef argument type mismatch

in the if statement.

I had all of the arrays declared as variants, but that also gave me an error when I tried to run the function.

Any help would be greatly appreciated.

sub sort()

Dim rowNo As Integer
Dim colNo As Integer
Dim PIList(6) As Long
Dim GSList(2) As Long
Dim FilterList(91) As Long
Dim currCellCont As Long

rowNo = 2
colNo = 2
PIList(6) = Array(0,1,2,3,4,5)
GSList(2) = Array(6,7)
FilterList(91) = Array(89752, 89753, 89754, 89755, 89756, 89757, 89758, 89759, 89760, 89761, 89762, 89763, 89764, 89765, 89766, 89767, 89768, 89769, 89770, 89771, 89772, 89773, 89774, 89775, 89776, 89777, 89778, 89779, 89780, 89781, 89782, 89783, 89784, 89785, 89786, 89787, 89788, 89789, 89790, 89791, 89792, 89793, 89794, 89795, 89796, 89797, 89798, 89799, 89800, 89801, 89802, 89803, 89804, 89805, 89806, 89807, 89808, 89809, 89810, 89811, 89812, 89813, 89814, 89815, 89816, 89817, 89818, 89819, 89820, 89821, 89822, 89823, 89824, 89825, 89826, 89827, 89828, 89829, 89830, 89831, 89832, 89833, 89834, 89835, 89836, 89837, 89838, 89839, 89840, 89841, 89842)
currCellCont = Cells(rowNo, colNo).Value

Do Until IsEmpty(currCellCont)
    If IsInArray(currCellCont, PIList) Then
        Cells(rowNo, 9).Value = "PI"
    ElseIf IsInArray(currCellCont, GSList) Then
        Cells(rowNo, 9).Value = "GS"
    ElseIf IsInArray(currCellCont, FilterList) Then
        Cells(roNo, colNo).Value = "Filter"
    End If
    currCellCont = Cells(rowNo + 1, colNo)
Loop

End Sub


Function IsInArray(lookingFor As Long, arr As Long) As Boolean
    IsInArray = Not IsError(Application.Match(lookingFor, arr, 0))
End Function
2
  • 1
    There's no array parameter in IsInArray
    – JohnyL
    Dec 8, 2017 at 19:06
  • 1
    That code has more problems than just the missing () on arr. PIList(6) = Array(0,1,2,3,4,5) is trying to assign a Variant array to the 6th position (7th position if it is zero-based) of a Long array - which will give a "Type mismatch" error.
    – YowE3K
    Dec 8, 2017 at 19:07

1 Answer 1

2

Use variant arrays:

Sub sort()

Dim rowNo As Integer
Dim colNo As Integer
Dim PIList() As Variant
Dim GSList() As Variant
Dim FilterList() As Variant
Dim currCellCont As Long

rowNo = 2
colNo = 2
PIList = Array(0, 1, 2, 3, 4, 5)
GSList = Array(6, 7)
FilterList = Array(89752, 89753, 89754, 89755, 89756, 89757, 89758, 89759, 89760, 89761, 89762, 89763, 89764, 89765, 89766, 89767, 89768, 89769, 89770, 89771, 89772, 89773, 89774, 89775, 89776, 89777, 89778, 89779, 89780, 89781, 89782, 89783, 89784, 89785, 89786, 89787, 89788, 89789, 89790, 89791, 89792, 89793, 89794, 89795, 89796, 89797, 89798, 89799, 89800, 89801, 89802, 89803, 89804, 89805, 89806, 89807, 89808, 89809, 89810, 89811, 89812, 89813, 89814, 89815, 89816, 89817, 89818, 89819, 89820, 89821, 89822, 89823, 89824, 89825, 89826, 89827, 89828, 89829, 89830, 89831, 89832, 89833, 89834, 89835, 89836, 89837, 89838, 89839, 89840, 89841, 89842)
currCellCont = ActiveSheet.Cells(rowNo, colNo).Value
With Worksheets("Sheet1") 'Change to your sheet if always the same or ActiveSheet if on various sheets.
    Do Until currCellCont = 0
        If IsInArray(currCellCont, PIList) Then
            .Cells(rowNo, 9).Value = "PI"
        ElseIf IsInArray(currCellCont, GSList) Then
            .Cells(rowNo, 9).Value = "GS"
        ElseIf IsInArray(currCellCont, FilterList) Then
            .Cells(roNo, colNo).Value = "Filter"
        End If
        rowNo = rowNo + 1
        currCellCont = .Cells(rowNo, colNo).Value
    Loop
End With


End Sub


Function IsInArray(lookingFor As Long, arr() As Variant) As Boolean
    IsInArray = Not IsError(Application.Match(lookingFor, arr, 0))
End Function
5
  • I guess, he's an expert on ActiveSheet theme) BTW, you didn't answer, why prefixing sheet is not enough...
    – JohnyL
    Dec 8, 2017 at 19:31
  • I am not sure what you mean by prefixing sheets. Dec 8, 2017 at 19:32
  • I mean, why prefixing with just "ActiveSheet" is not enough?
    – JohnyL
    Dec 8, 2017 at 19:34
  • You mean by leaving it blank and letting vbe assume the activesheet? Mat's Mug did answer that in his comment to you on the last string. Normally in this situation I would use With ActiveSheet or if the sheet was specific I would use With Worksheets("Sheet1") then I did not need to manually activate the sheet and could run the code from anywhere. Then just prefix the ranges with .. But in this specific case it was just me being lazy and using copy/paste to get the minimum amount of stability. Dec 8, 2017 at 19:39
  • Let us continue this discussion in chat.
    – JohnyL
    Dec 8, 2017 at 19:44

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.