【问题标题】:Compare two ranges on different sheets and if the values match copy range比较不同工作表上的两个范围以及值是否与复制范围匹配
【发布时间】:2016-01-13 15:05:00
【问题描述】:

我有两张表(当前月份和上个月),每行大约有 4000 个不同的帐号和详细信息。比较帐号,如果当前月份表中的帐号不在上个月表中,则以黄色突出显示。这部分在下面的代码中对我有用。

我无法工作的部分。如果帐号匹配,则将上个月表中匹配帐号的 H 到 N 列复制到当前月份表中的 H 到 N 列。我在尝试定义 LastRow 变量时遇到运行时错误 9 - 下标超出范围,但我也不确定该错误下方的循环是否也能正常工作。

这里是代码;

Sub RangeCompare()
'   The range Previous Month is the baseline so if the range Current Month has a value that is not in the Previous Month it is highlighted
'   If the Previous Month range has a value that is in Current Month then copy Previous Months columns H to N to Current Month columns H to N
'   ACCOUNT NUMBERS SELECTED MUST BE IN COLUMN A, IF NOT THE COPY/PASTE WILL NOT WORK!

'   Warning to the end user that column A must contain the Account Numbers or this will cause the copy part of the macro to fail
    MsgBox "!! Column A must contain the Account Numbers !!"

    Dim PrevMonth As Range, CurMonth As Range, c As Range, TempVal As Range
    Dim CurSheet As String, PrevSheet As String
    Dim LastRow1 As Long

    Set PrevMonth = Application.InputBox("Select appropriate sheet and all accounts:", Title:="Select Previous Months Accounts", Type:=8)
'    next variable needed so VBA knows what the Previous worksheet name is, this is needed for the copying section of the macro
    PrevSheet = ActiveSheet.Name
    If PrevMonth Is Nothing Then
        MsgBox "No range selected. Ending program..."
        Exit Sub
    End If

    Set CurMonth = Application.InputBox("Select appropriate sheet and all accounts:", Title:="Select Current Months Accounts", Type:=8)
    ' next variable needed so VBA knows what the Current worksheet name is, this is needed for the copying section of the macro
    CurSheet = ActiveSheet.Name
    LastRow2 = Selection.Rows.Count
    If CurMonth Is Nothing Then
        MsgBox "No range selected. Ending program..."
        Exit Sub
    End If


'   Highlight cells not in PrevMonth to yellow Colorindex = 6
'   This loop works :-)
    For Each c In CurMonth.Cells
        If Application.WorksheetFunction.CountIf(PrevMonth, c.Value) = 0 Then
            c.Interior.ColorIndex = 6
        End If
    Next c

'    Copy columns H:N from previous month sheet to H:N in current months sheet if Account numbers match

   LastRow1 = Sheets("PrevSheet").Range("PrevMonth").Rows.Count
'  The above gives runtime error 9 - subscript out of range
'  I don't know if the loop below will work as I can't define LastRow1 due to the error above.
    For sRow = 2 To LastRow1
        TempVal = Sheets(PrevSheet).Range(sRow, "H").Range(sRow, "N")
             If Sheets("CurSheet").Cells(sRow, 1).Text = Sheets("PrevSheet").Cells(sRow, 1).Text Then
                    Sheets(CurSheet).Range(sRow, "H").Range(sRow, "N") = TempVal
             End If
    Next sRow

End Sub

【问题讨论】:

  • LastRow1 = Sheets("PrevSheet").Range("PrevMonth").Rows.Count 行出现“运行时错误 9 - 下标超出范围”的错误意味着您的工作簿没有名为“PrevSheet”的工作表或该工作表没有有一个名为“PrevMonth”的命名范围。仔细检查您的工作表名称和命名范围。
  • 看起来你可能只想要Sheets("PrevSheet").Range("PrevMonth")而不是PrevMonth
  • 感谢tigeravatar,我在代码中进一步定义了PrevSheet,如下所示; "PrevSheet = ActiveSheet.Name
  • 对,您已经定义了the worksheet variable PrevSheet,但您只需使用带有Sheets("PrevSheet") 的字符串。当它是这样的文字字符串时,您不是在引用预定义的变量,而是在告诉 Excel 查找一个 实际命名“PrevSheet” 的工作表
  • 所以我应该删除 "Sheets("PrevSheet") 条目并只使用 LastRow1 = Range("PrevMonth").Rows.Count?

标签: excel vba


【解决方案1】:

已更新以便在 VBA 中使用 INDEX/MATCH 查找帐号,因为工作表名称的名称将发生变化(我假设):

Dim PrevSheet, CurSheet As String

PrevSheet = "PrevSheet"
CurSheet = "CurSheet"

CurEnd = Sheets(CurSheet).Cells(Rows.Count, 1).End(xlUp).Row
PrevEnd = Sheets(PrevSheet).Cells(Rows.Count, 1).End(xlUp).Row

For sRow = 2 To CurEnd
Sheets(CurSheet).Range("H" & sRow).value = WorksheetFunction.Index(Sheets(PrevSheet).Range("H2:H" & PrevEnd), WorksheetFunction.Match(Sheets(CurSheet).Range("A" & sRow), Sheets(PrevSheet).Range("A2:A" & PrevEnd), 0))
Sheets(CurSheet).Range("I" & sRow).value = WorksheetFunction.Index(Sheets(PrevSheet).Range("I2:I" & PrevEnd), WorksheetFunction.Match(Sheets(CurSheet).Range("A" & sRow), Sheets(PrevSheet).Range("A2:A" & PrevEnd), 0))
Sheets(CurSheet).Range("J" & sRow).value = WorksheetFunction.Index(Sheets(PrevSheet).Range("J2:J" & PrevEnd), WorksheetFunction.Match(Sheets(CurSheet).Range("A" & sRow), Sheets(PrevSheet).Range("A2:A" & PrevEnd), 0))
Sheets(CurSheet).Range("K" & sRow).value = WorksheetFunction.Index(Sheets(PrevSheet).Range("K2:K" & PrevEnd), WorksheetFunction.Match(Sheets(CurSheet).Range("A" & sRow), Sheets(PrevSheet).Range("A2:A" & PrevEnd), 0))
Sheets(CurSheet).Range("L" & sRow).value = WorksheetFunction.Index(Sheets(PrevSheet).Range("L2:L" & PrevEnd), WorksheetFunction.Match(Sheets(CurSheet).Range("A" & sRow), Sheets(PrevSheet).Range("A2:A" & PrevEnd), 0))
Sheets(CurSheet).Range("M" & sRow).value = WorksheetFunction.Index(Sheets(PrevSheet).Range("M2:M" & PrevEnd), WorksheetFunction.Match(Sheets(CurSheet).Range("A" & sRow), Sheets(PrevSheet).Range("A2:A" & PrevEnd), 0))
Sheets(CurSheet).Range("N" & sRow).value = WorksheetFunction.Index(Sheets(PrevSheet).Range("N2:N" & PrevEnd), WorksheetFunction.Match(Sheets(CurSheet).Range("A" & sRow), Sheets(PrevSheet).Range("A2:A" & PrevEnd), 0))
Next sRow

【讨论】:

  • 感谢 Jordan,但它在“TempVal = Sheets(PrevSheet).Range("H" & sRow & ":N" & sRow)" 当我使用 F8 按钮单步执行代码时
  • 向乔丹道歉。我现在已将 Tempval 设置为 Variant,它现在因“运行时错误 5 - Invalid procedure call or argument on the "If Sheets(CurSheet).Range("A" & sRow).Value = Sheets(PrevSheet) 而失败).Cells("A" & sRow).Value Then" 语句
  • 对不起,该行应该是If Sheets(CurSheet).Range("A" & sRow).Value = Sheets(PrevSheet).Range("A" & sRow).Value Then - 现在已在答案中进行了修改。它以前提到Cells("A" & sRow).Value 而不是Range("A" & sRow).Value
  • 此外,此代码仅在 PrevSheetCurSheet 以相同方式格式化时才有效,例如您的问题中提到的“帐号”都在两个工作表上的同一行中。
  • 感谢 Jordan,宏现在运行没有错误,但没有从 PrevSheet 复制到 CurSheet。很抱歉这么痛苦。
猜你喜欢
  • 2021-08-08
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多