-1

我有 3 个文件

我需要做的(必须使用 VBA excel 完成,因为它是更大宏的一部分)是查看文档 A 中的 A 列,并且对于文档 B 列中存在匹配项的每个实例,AI 需要检查如果两个文档的 B 列中的值是 = "rf" 如果是,我需要检查工作簿 A 的 C 列中的值是否 > C 列。

如果这一切都是真的,我想将工作簿 A 列 A 的值发布到同一单元格地址中的工作簿 C 列 C 中。

我有下面的代码,但它在第一个相交循环上返回一个错误,上面写着“对象'_global'的方法'相交'失败。任何想法为什么会发生这种情况?

Public Sub RFErrorProof()

Dim input1 As String
Dim input2 As String
Dim rCell As Range
Dim rFound As Range
Dim rNext As Range

Input3 = "ReportCompare.xls"
input1 = Workbooks(Input3).Worksheets("Sheet4").Range("A4").Value
input2 = Workbooks(Input3).Worksheets("Sheet4").Range("A3").Value

'Loop through column A in doc A
    For Each rCell In Intersect(Workbooks(input2).Worksheets("LocalesMallContratos").UsedRange.Columns(2), Workbooks(input1).Worksheets("LocalesMallContratos").UsedRange.Columns(2)).Cells
    'Skip cells where column B is not RF
    If rCell.Offset(0, 3).Value = "RF" Then

        'See if that exists in doc B
        Set rFound = Nothing
        Set rFound = Workbooks(input1).Columns(2).Find(rCell.Value, , xlValues, xlWhole)

        'If it's in doc B
        If Not rFound Is Nothing Then

            'If column B doc B is RF and doc A is greater than doc B, then write it
            If rFound.Offset(0, 3).Value = "RF" Then
                If rCell.Offset(0, 14).Value > rFound.Offset(0, 14).Value Then
                    Set rNext = Workbooks(Input3).Cells(Workbooks(Input3).Sheets("sheet1").Rows.Count, 1).End(xlUp).Offset(1, 0)
                    rNext.Value = rCell.Value
                End If
            Else
                'If column B doc B is not RF, write it
                Set rNext = Workbooks(Input3).Cells(Workbooks(Input3).Sheets("sheet1").Rows.Count, 1).End(xlUp).Offset(1, 0)
                rNext.Value = rCell.Value
            End If
        End If
    End If
Next rCell


End Sub
4

2 回答 2

0

始终在数据副本上测试代码。先做好备份。

Public Sub MakeDocuC()

    Dim rCell As Range
    Dim rFound As Range
    Dim rNext As Range

    'Loop through column A in doc A
    For Each rCell In Intersect(docuA.UsedRange, docuA.Columns(1)).Cells
        'Skip cells where column B is not RF
        If rCell.Offset(0, 1).Value = "RF" Then

            'See if that exists in doc B
            Set rFound = Nothing
            Set rFound = docuB.Columns(1).Find(rCell.Value, , xlValues, xlWhole)

            'If it's in doc B
            If Not rFound Is Nothing Then

                'If column B doc B is RF and doc A is greater than doc B, then write it
                If rFound.Offset(0, 1).Value = "RF" Then
                    If rCell.Offset(0, 2).Value > rFound.Offset(0, 2).Value Then
                        Set rNext = docuC.Cells(docuC.Rows.Count, 1).End(xlUp).Offset(1, 0)
                        rNext.Value = rCell.Value
                    End If
                Else
                    'If column B doc B is not RF, write it
                    Set rNext = docuC.Cells(docuC.Rows.Count, 1).End(xlUp).Offset(1, 0)
                    rNext.Value = rCell.Value
                End If
            End If
        End If
    Next rCell

End Sub
于 2013-05-13T13:40:40.747 回答
0

我 99% 确信这可以以极大的方式简化,但我只是准确地研究了你的逻辑并想出了这个公式(我认为没有必要为此使用 VBA)。

只需将此公式复制到工作表 C 的单元格 A1 中并复制/粘贴即可:

=IF(A!A1=B!A1,IF(A!B1="RF",IF(B!B1<>"RF",A!A1,IF(A!C1>B!C1,A!A1,"")),""),"")

希望这能解决问题!

于 2013-05-13T13:22:51.707 回答