IsInArray函数错误:类型不匹配VBA

IsInArray函数错误:类型不匹配VBA,vba,excel,Vba,Excel,当我运行这个时,它会给我一个错误 ByRef参数类型不匹配 在if语句中 我将所有数组声明为变体,但在尝试运行该函数时,这也给了我一个错误 任何帮助都将不胜感激 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 =

当我运行这个时,它会给我一个错误

ByRef参数类型不匹配

在if语句中

我将所有数组声明为变体,但在尝试运行该函数时,这也给了我一个错误

任何帮助都将不胜感激

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
使用变量数组:

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
使用变量数组:

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

isinarray中没有数组参数,代码的问题不仅仅是
arr
上缺少的
()
PIList(6)=数组(0,1,2,3,4,5)
正在尝试将一个
变量
数组分配到
数组的第6个位置(如果是从零开始的话,则为第7个位置)-这将产生“类型不匹配”错误。Isinarray中没有任何数组参数表明
上缺少的
()
PIList(6)=数组(0,1,2,3,4,5)
正在尝试将一个
变量
数组分配到
数组的第6位(如果是从零开始的,则为第7位)-这将导致“类型不匹配”错误。我猜,他是ActiveSheet主题方面的专家)顺便说一句,您没有回答,为什么在工作表前面加前缀是不够的…我不知道你所说的在工作表前面加前缀是什么意思。我的意思是,为什么只在“活动工作表”前面加前缀是不够的?你的意思是让它留空,让vbe承担活动工作表?马特的马克杯在他对你的最后一条评论中回答了这个问题。通常在这种情况下,我会将
与ActiveSheet一起使用
,或者如果工作表是特定的,我会将
与工作表(“Sheet1”)
一起使用,那么我不需要手动激活工作表,可以从任何地方运行代码。然后只需在范围前面加上
。但在这种特殊情况下,只是我懒惰,使用复制/粘贴来获得最低限度的稳定性。让我们来看看。我猜,他是ActiveSheet主题方面的专家)顺便说一句,你没有回答,为什么在工作表前面加前缀是不够的……我不确定你所说的在工作表前面加前缀是什么意思。我的意思是,为什么只在“ActiveSheet”前面加前缀还不够吗?你的意思是让它留空,让vbe承担活动表?马特的马克杯在他对你的最后一条评论中回答了这个问题。通常在这种情况下,我会将
与ActiveSheet一起使用
,或者如果工作表是特定的,我会将
与工作表(“Sheet1”)
一起使用,那么我不需要手动激活工作表,可以从任何地方运行代码。然后只需在范围前面加上
。但在这种特殊情况下,我只是懒惰,使用复制/粘贴来获得最低限度的稳定性。