【发布时间】: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?