Excel 为什么我需要运行时错误424对象?

Excel 为什么我需要运行时错误424对象?,excel,vba,Excel,Vba,我对VBA相当陌生。我正试图编写一个程序来运行大量不同格式的零件号,并对这些零件号进行分类,如下所示: 12A3-4 1B-4 2B-6 A12B 然后让我的程序找到这些类型的所有格式并返回并计数,如下所示:(注意数字现在用#表示) 但是我得到了一个运行时错误,我似乎无法找到它的来源 我的程序如下。可能还有其他错误 Sub CheckPartNumbers() ' Select cell A2, where data begins Range("A2").Select ' Declare

我对VBA相当陌生。我正试图编写一个程序来运行大量不同格式的零件号,并对这些零件号进行分类,如下所示:

12A3-4
1B-4
2B-6
A12B
然后让我的程序找到这些类型的所有格式并返回并计数,如下所示:(注意数字现在用#表示)

但是我得到了一个运行时错误,我似乎无法找到它的来源

我的程序如下。可能还有其他错误

 Sub CheckPartNumbers()

' Select cell A2, where data begins
Range("A2").Select
' Declare variable for cell location of our output
Dim Cell As Range
Set Cell = ActiveSheet.Range("C2")

' Set Do loop to stop when an empty cell is reached.
Do Until IsEmpty(ActiveCell)
' Initialize vairable of type string to ""
Dim partsFormat As String
partsFormat = ""
' Run through each character of row
For i = 1 To Len(ActiveCell.Value)
    Dim thisChar As String
    thisChar = Mid(ActiveCell.Value, i, 1)
    ' if thisChar is a letter
    If IsLetter(thisChar) Then
    partsFormat = partsFormat & thisChar
    ' if thischar is a number
    ElseIf IsNumeric(thisChar) Then
    partsFormat = partsFormat & "#"
    ' if dash
    ElseIf thisChar = "-" And (Len(ActiveCell.Value) - Len(Replace(ActiveCell.Value, "-", ""))) > 1 Then
    partsFormat = partsFormat & thisChar
    Else
    i = Len(ActiveCell.Value)
    End If
Next i
' Check if partsFormat already exists in results with Match
Dim myLocation As Range
Set myLocation = Application.Match(partsFormat, Range("C2:D1"))
' If no, result will give error, so add partsFormat and make count 1
If IsError(myLocation) Then
Range(Cell) = partsFormat
Range(Cell).Offset(0, 1) = 1
Cell = Cell.Offset(1, 0)
' If yes, add 1 to appropriate cell
Else
myLocation.Offset(0, 1) = myLocation.Offset(0, 1).Value + 1
End If

' Run through next row
ActiveCell.Offset(1, 0).Select
Loop

End Sub
感谢您的帮助

编辑: 我有很多错误,所以我的这段代码被更新了:

Dim myLocation As Variant 
myLocation = Application.Match(partsFormat, Range("C1").EntireColumn) 
' If no, result will give error, so add partsFormat and make count 1 
If IsError(myLocation) Then
Cell = partsFormat
Cell.Offset(0, 1) = 1
Cell = Cell.Offset(1, 0) 
' If yes, add 1 to appropriate cell 
Else 
'myLocation.Offset(0, 1) = myLocation.Offset(0, 1).Value + 1 
End If

424错误由以下两行引起:

Dim myLocation As Range
Set myLocation = Application.Match(partsFormat, Range("C2:D1"))
您已将myLocation声明为
范围
。然后您试图将其设置为一个数字,
MATCH
返回的是数字,而不是范围。所需的对象是一个范围

编辑:

为了只计算C列中出现的
partsFormat
,请使用以下方法:

Dim partsFormat As String
Dim partsFormatCount As Long

partsFormat = "Part"
partsFormatCount = Application.WorksheetFunction.CountIf(Range("C:C"), partsFormat)

显然,您必须将其插入代码中正确的位置。

424错误是由以下两行引起的:

Dim myLocation As Range
Set myLocation = Application.Match(partsFormat, Range("C2:D1"))
您已将myLocation声明为
范围
。然后您试图将其设置为一个数字,
MATCH
返回的是数字,而不是范围。所需的对象是一个范围

编辑:

为了只计算C列中出现的
partsFormat
,请使用以下方法:

Dim partsFormat As String
Dim partsFormatCount As Long

partsFormat = "Part"
partsFormatCount = Application.WorksheetFunction.CountIf(Range("C:C"), partsFormat)

显然,您必须将其插入代码中正确的位置。

您是否已经使用了调试器?请包括您正在接收的运行时错误,以及如果可能的话,它在哪一行中断。我一直在调试,虽然我的错误从未突出显示一行,但我很确定以“Set myLocation”开头的行与Match函数是罪魁祸首。我只是不知道为什么。正如我在标题中所说,错误是运行时错误424。您是否已经使用了调试器?请包括您正在接收的运行时错误,以及如果可能的话,它在哪一行中断。我一直在调试,虽然我的错误从未突出显示一行,但我很确定以“Set myLocation”开头的行与Match函数是罪魁祸首。我只是不知道为什么。正如我在标题中所说,错误是运行时错误424。我认为这是位置。。。如何使用函数给我一个范围值?范围值在哪一列?另外,我注意到您试图匹配多个列或行,这是行不通的。我认为您需要更好地描述您的目标。我试图检查的值(partsFormat)应该在C列中。因此,我只需要在C列中搜索该格式。如果它存在,我只想在计数中加1。如果它不存在,我想将它添加到C列的底部,这样下次找到partsFormat时,它的计数将增加到2。我以为这就是位置。。。如何使用函数给我一个范围值?范围值在哪一列?另外,我注意到您试图匹配多个列或行,这是行不通的。我认为您需要更好地描述您的目标。我试图检查的值(partsFormat)应该在C列中。因此,我只需要在C列中搜索该格式。如果它存在,我只想在计数中加1。如果它不存在,我想将它添加到C列的底部,以便下次找到partsFormat时,它将把计数增加到2。