比较不同工作表上的两个范围,以及这些值是否符合复印范围

我有两张表(本月和上个月),每行约有4000个不同的帐号和详细信息。 账户号码进行比较,如果“本月”表中的账号不在“上个月”表中,则用黄色突出显示。 这部分工作在下面的代码中。

我无法工作的部分。 如果帐号匹配,则将当前月份表中匹配的帐号从上一个月工作表H列复制到N列。 我得到运行时错误9 – 下标超出范围,当我试图定义LastRowvariables,但我也不确定如果低于该错误的循环将工作。

这是代码;

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 

更新为了在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