-1

所以我想要做的是在工作簿 A 中有一个宏,它打开工作簿 B 和 C。之后,它通过工作簿 B 和 C 的 A 列,并且两个值相等,它从工作簿 C 中获取值并将其粘贴到工作簿 A 的 A 列。

我已经编写了下面的代码,但是如果您认为以另一种方式更容易,请随时编写自己的代码。谢谢你,请帮助我:)

Sub ReportCompareAlta()
'
' ReportCompareAlta Macro
' Adds instances where column D is "ALTA"

    Dim varSheetA As Variant
    Dim varSheetB As Variant
    Dim varSheetC As Variant
    Dim StrValue As Variant
    Dim strRangeToCheck As String
    Dim iRow As Long
    Dim iCol As Long
    Dim WbkA As Workbook
    Dim WbkB As Workbook
    Dim WbkC As Workbook
    Dim counter As Long

    Set WbkA = Workbooks.Open(Filename:="G:\Reporting\AH_MISSE_FEB2013.xls")
    Set WbkB = Workbooks.Open(Filename:="G:\Reporting\AH_MISSE_MAR2013.xls")
    Set WbkC = Workbooks.Open(Filename:="G:\Reporting\ReportCompare.xls")
    Set varSheetA = WbkA.Worksheets("LocalesMallContratos")
    Set varSheetB = WbkB.Worksheets("LocalesMallContratos")
    Set varSheetC = WbkC.Worksheets("Sheet1")


    strRangeToCheck = "A1:IV65536"

    Debug.Print Now
    varSheetA = WbkC.Worksheets("Sheet2").Range(strRangeToCheck) 'may be confusing code here
    varSheetB = WbkC.Worksheets("Sheet3").Range(strRangeToCheck) 'may be confusing code here
    Debug.Print Now

    counter = 0

    For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)        
            If varSheetB(iRow, "B") = varSheetA(iRow, "B") & varSheetB(iRow, "B") <> "GERENCIA" & varSheetB(iRow, "B").Value <> "" & varSheetB(iRow, "D") = "ALTA" Then
                StrValue = ""
                varSheetB.Range("iRow:B").Select 
                Selection = StrValue
                ActiveSheet = varSheetC
                Range("A1").Select
                Selection.Offset(counter, 0).Value = StrValue
                counter = counter - 1

            Else
                MsgBox ("Done")
            End If         
    Next iRow

End Sub
4

2 回答 2

2

您需要在代码中使用单词“AND”而不是“&”符号。使用 '&' 只是连接值,这将导致您的 if 语句失败。

于 2013-05-02T10:50:07.933 回答
1

正如 Mat Richardson 指出的那样,我看到了一些明显的错误,使用&is not equivalentAND运算符的简写,它是一个连接符,当你说这不太可能是你想要的:

If varSheetB(iRow, "B") = varSheetA(iRow, "B") & varSheetB(iRow, "B") <> "GERENCIA" & varSheetB(iRow, "B").Value <> "" & varSheetB(iRow, "D") = "ALTA" Then

这给我带来了另一个错误:

varSheetB(以及 A 和 C)是 Variant/Array 变量。你不能索引这些,iRow, "B"因为你不能使用非数字索引。也许你的意思是(iRow, 2)。

在相关说明中:varSheetB.Range("iRow:B").Select这也会失败,因为您不能.Select使用 Variant。这不是Range 变量。此外,对于 Variant 数组Range 变量iRow:B都不正确。另外,此时,不再是对象变量。varSheetBWorksheet

这可能给我带来了最大的错误:您正在使用变量varSheetA,varSheetBvarSheetC来表示(在此代码中的不同时间)aWorksheet Object和 Variant 值数组。这很令人困惑,并且可能会导致您出现上述错误。一个变量不能同时是这两种东西,所以你需要把你的变体当它们是变体时像变体一样对待,当它们是工作表时像工作表一样对待,或者更好:对工作表使用工作表变量,对数组使用变量,不要将同一个变量用于多种目的。

Sub ReportCompareAlta()
'
' ReportCompareAlta Macro

Dim varSheetA As Worksheet
Dim varSheetB As Worksheet
Dim varSheetC As Worksheet
Dim RangeToCheck As Range
Dim cl as Range
Dim iRow As Long
Dim iCol As Long
Dim WbkA As Workbook
Dim WbkB As Workbook
Dim WbkC As Workbook
Dim counter As Long

Set WbkA = Workbooks.Open(Filename:="G:\Reporting\AH_MISSE_FEB2013.xls")
Set WbkB = Workbooks.Open(Filename:="G:\Reporting\AH_MISSE_MAR2013.xls")
Set WbkC = Workbooks.Open(Filename:="G:\Reporting\ReportCompare.xls")
Set varSheetA = WbkA.Worksheets("LocalesMallContratos")
Set varSheetB = WbkB.Worksheets("LocalesMallContratos")
Set varSheetC = WbkC.Worksheets("Sheet1")


Set RangeToCheck = varSheetA.Range("A1:A65536") '## I change this because you only indicate you want to compare column A ##'

counter = 0

'## just loop over the cells in the range. ##'
'## This is not the most efficient, but it is the easiest ##'
For each cl in RangeToCheck  

    '## Do your comparison here, e.g: ##'
    '## Ignore cells where .Offset(0,3).Value = "ALTA" Or cl.Value = "" ##'
    If not cl.Offset(0,3).Value = "ALTA" Or Not cl.Value = vbNullString Then
        If Not cl.Value = varSheetB.Range(cl.Address).Value Then
           '## The values are not equal, so do something:
                varSheetC.Range(cl.Address) = "not equal"
           counter = counter+1
        Else:
           '## The values are equal, so do something else:
            varSheetC.Range(cl.Address) = "equal"
        End If
   End If
Next


MsgBox "Done! There were " & counter & " mismatch values", vbInformation
End Sub
于 2013-05-02T11:49:10.987 回答