变量赋值时发生Excel VBA 1004错误

时间:2019-04-28 17:16:37

标签: excel vba

我正在编写一个脚本/宏来比较现有电子表格和导入电子表格之间的值。我正在尝试找出任何变化。作为此过程的一部分,我正在旧表的校样表上搜索每个项目的名称和ID。当我尝试在循环中为变量分配名称和ID时,出现1004错误

我尝试更改引用单元格的方式以及引用单元格的顺序,但这似乎无关紧要,同一行不断导致错误

引发错误的行是id分配。

            Dim name As String
            Dim id As String


            name = Sheets(proofsheet).Range(destItemColumn & m).Text
            id = Sheets(proofsheet).Range(destIdColumn & m).Text

大部分操作在这里完成:

proofLRow = Sheets(proofsheet).Cells(Rows.Count, 1).End(xlUp).Row
    For m = 2 To proofLRow
        If Not (InStr(Sheets(proofsheet).Range(destIdColumn1 & m), "TBD") = 0) Then
            Dim name As String
            Dim id As String


            name = Sheets(proofsheet).Range(destItemColumn & m).Text
            id = Sheets(proofsheet).Range(destIdColumn & m).Text

            Dim idFound, nameFound As Boolean
            idFound = False
            nameFound = False
            For k = 2 To prevDataLRow
                searchId = Sheets(sheet).Range(sourceIdColumn & k)
                searchName = Sheets(sheet).Range(sourceItemColumn & k)
                If (searchId = id) Then
                    idFound = True
                End If
                If (searchName = name) Then
                    nameFound = True
                End If
                If (nameFound Or idFound) Then
                    Exit For
                End If

            Next

            If idFound Then
                Sheets(proofsheet).Range(destFlagColumn & m).Value = ""
            ElseIf nameFound Then
                Sheets(proofsheet).Range(destFlagColumn & m).Value = "Replace"
            Else
                Sheets(proofsheet).Range(destFlagColumn & m).Value = "Retired"
            End If

        End If

    Next

我至少应该能够将扫描的数据写入消息框以进行比较,但是我一直收到此1004错误,或者当我尝试将搜索放入函数中时,我将收到有关以下内容的编译错误类型不匹配。

0 个答案:

没有答案