Excel 应用程序定义或对象定义错误

Excel 应用程序定义或对象定义错误,excel,excel-2010,vba,Excel,Excel 2010,Vba,全部, 我收到了我编写的私有子系统的错误“应用程序定义或对象定义错误”。代码如下: Private Sub CommandButton3_Click() Dim MyLastRow As Long Dim i As Long Dim cellmatch 'Find the last row MyLastRow = Cells(Rows.Count, "A").End(xlUp).Row 'Define our comparison cellmatch = Application.Match

全部,

我收到了我编写的私有子系统的错误“应用程序定义或对象定义错误”。代码如下:

Private Sub CommandButton3_Click()

Dim MyLastRow As Long
Dim i As Long
Dim cellmatch

'Find the last row
MyLastRow = Cells(Rows.Count, "A").End(xlUp).Row

'Define our comparison
cellmatch = Application.Match(Cells(i, "A").Value, Range(Cells(i, "C")).Value, 0)

'Compare Raw Data cell to Stock column and find a match
For i = 2 To MyLastRow
    If IsError(cellmatch) Then
        Cells(i, 2) = "Not in Stock"
    Else
        Cells(i, 2) = "-"
    End If
Next i

End Sub
我尝试了在论坛上找到的一些东西,比如我们指定了工作表

Application.WorksheetFuncion.Match.....
我还尝试指向单元格或区域,例如:

Range(.Cells(i,"C"))....
或 .Match(.Cells(i,“A”)

但我总是犯同样的错误。所有这些都发生在同一张纸上,我不想做任何像复制这样的花哨事。我只是问,如果没有找到匹配项,那么就这样标记,否则,用破折号标记它(这样做是为了清晰)。我确信这是一件非常简单的事情,但我对用VBA编码还不熟悉。非常感谢您的帮助


谢谢

我相信这就是你想要的:

Option Explicit

Private Sub CommandButton3_Click()

Dim lngRow As Long
Dim rngFound As Range
Dim lngLastRow As Long
Dim shtCurrent As Worksheet

'Set the sheet to work on
Set shtCurrent = ThisWorkbook.Worksheets("Sheet1")

With shtCurrent
    'Find the last row
    lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    'Exit if the last row is 2 or smaller
    If lngLastRow <= 2 Then
        MsgBox "Nothing to compare!" & Chr(10) & "Aborting..."
        Exit Sub
    End If

    'Compare Raw Data cell to Stock column and find a match
    For lngRow = 2 To lngLastRow
        'Only compare if there is something in column A to compare
        If .Cells(lngRow, "A").Value2 <> vbNullString Then
            'This is the actual MATCH / FIND
            Set rngFound = .Range("C:C").Find(What:=.Cells(lngRow, "A").Value2, LookIn:=xlValues, LookAt:=xlWhole)
            'Evaluate the result of the FIND = rngFound
            If rngFound Is Nothing Then
                .Cells(lngRow, 2).Value2 = "Not in Stock"                           'not found
            Else
                .Cells(lngRow, 2).Value2 = "In stock in row " & rngFound.Row        'found
            End If
        End If
    Next lngRow
End With

End Sub
选项显式
私有子命令按钮3_单击()
长得一样长
暗淡的rngFound As范围
暗淡的玻璃和长的一样
将SHTCCurrent变暗为工作表
'设置要处理的工作表
设置SHTCCurrent=ThisWorkbook.Worksheets(“Sheet1”)
短电流
“找到最后一行
lngLastRow=.Cells(.Rows.Count,“A”).End(xlUp).Row
'如果最后一行为2或更小,则退出

如果lngLastRow您的代码需要更改此代码行

 cellmatch = Application.Match(Cells(i, "A").Value, Range(Cells(i, "C")).Value, 0)

 'Adjust Sheetname as per your requirements instead of "Sheet1"
     cellmatch = Application.Match(Cells(i, "A").Value, Worksheets("Sheet1").Columns(3), 0)
编辑

由于以下代码片段,程序中出现了主要问题

Range(Cells(i, "C")).Value
如果我们参考MSDN文档

它提到了使用正确语法的例子。 典型的例子是

Set r = Range("myRange") 
For n = 1 To r.Rows.Count 
    If r.Cells(n, 1) = r.Cells(n + 1, 1) Then 
        MsgBox "Duplicate data in " & r.Cells(n + 1, 1).Address 
    End If 
Next n
因此,它转换为
Range(“myRange”)。单元格(n,1)

而不是

Range(Cells(i, "C"))
它将给出快照中所示的正确结果


您的单元格匹配线不在循环中,而是使用了
i
?您的匹配函数也只是将一个单元格与另一个单元格进行比较,但它看起来应该是检查列而不是单元格……如果将对应行更改为~cellmatch=Application.match(单元格(i,“a”)。值,工作表,则可能不会出现错误(“表1”)。第(3)列和第(0)列~。我想@Macro-Man也指出了你的编码中同样的缺点。@MacroMan是的,它应该检查一个单元格和一列。它应该询问列中是否有一个单元格与活动单元格匹配。如果没有,那么它会这样说。否则,它会说它是一个“-”。@micstr Oh-Duh!谢谢。是的,应该是这样的en在我的FOR循环中。:P我将进行更正。