我正在尝试创建一个宏,该宏在我们收到的数据表/模板上运行大量验证(比我目前的代码要多得多)。
当发现问题时,宏将单元格的填充格式化为红色,然后需要将问题写入新工作表中的错误报告。错误报告需要在Col A中写入错误行,并在Col B中写入错误的简要说明。
我已经将下面的宏做了一半工作,因为它用红色填充所有不正确的单元格,但我无法正确写入错误报告。
1)当我使用Activecell.row时,它返回我在运行宏之前所处的活动单元格行而不是循环中的行(iRow)
2)我无法将错误写在彼此之下,即使我在尝试使用lastARow参数时仍保持覆盖,但它没有改变。不确定是否有我遗漏的东西,或者我是否需要循环中的另一个循环(我也试过这个)
Sub CheckValidations()
Dim iRow As Long, lastRow As Long, firstRow As Long, ARow As Long, firstARow As Long, lastARow As Long
Application.Calculation = xlManual
Application.DisplayStatusBar = False
Application.ScreenUpdating = False
lastRow = Sheets("Data").Range("B" & Rows.Count).End(xlUp).Row
firstRow = 14
lastARow = Sheets("Errors").Range("A" & Rows.Count).End(xlUp).Row
'For each row check validations in specific columns
For iRow = lastRow To firstRow Step -1
'Check SubFunction Exists, if not highlight cell and write issue to
error report
If Sheets("Data").Cells(iRow, 2) <> "CRO & Admin" And Sheets("Data").Cells(iRow, 2) <> "Operational Risk" And Sheets("Data").Cells(iRow, 2) <> "Global Risk Analytics" _
And Sheets("Data").Cells(iRow, 2) <> "Risk Strategy" And Sheets("Data").Cells(iRow, 2) <> "Security & Fraud Risk" And Sheets("Data").Cells(iRow, 2) <> "Wholesale & Market Risk" _
And Sheets("Data").Cells(iRow, 2) <> "RBWM Risk" And Sheets("Data").Cells(iRow, 2) <> "Indirects" And Sheets("Data").Cells(iRow, 2) <> "Run Risk Like a Business" _
And Sheets("Data").Cells(iRow, 2) <> "Location Optimisation" Then
Sheets("Data").Cells(iRow, 2).Interior.Color = RGB(255, 0, 0) 'Red
Sheets("Errors").Cells(lastARow, 1).Value = ActiveCell.Row 'Activecell.row returns cell from before macro was initiated, not iRow
Sheets("Errors").Cells(lastARow, 2).Value = "Value not in dropdown/allowed"
End If
'Check Proj Start date not before 01/07/2015, if it is wirte isue to error report
If Sheets("Data").Cells(iRow, 6) < DateSerial(2015, 7, 1) Then
Sheets("Data").Cells(iRow, 6).Interior.Color = RGB(255, 0, 0)
Sheets("Errors").Cells(lastARow, 1).Value = ActiveCell.Row 'Activecell.row returns cell from before macro was initiated, not iRow
Sheets("Errors").Cells(lastARow, 2).Value = "Start date before 01/07/2015"
End If
'Check Proj End date not after 31/12/2017, if it is wirte isue to error report
If Sheets("Data").Cells(iRow, 6) < DateSerial(2017, 12, 31) Then
Sheets("Data").Cells(iRow, 6).Interior.Color = RGB(255, 0, 0)
Sheets("Errors").Cells(lastARow, 1).Value = ActiveCell.Row 'Activecell.row returns cell from before macro was initiated, not iRow
Sheets("Errors").Cells(lastARow, 2).Value = "Start date before 01/07/2015"
End If
Next iRow
Application.Calculation = x1Automatic
Application.DisplayStatusBar = True
Application.ScreenUpdating = True
End Sub
非常感谢任何帮助,提前谢谢。
答案 0 :(得分:0)
首先,我个人不喜欢在vba代码中间引用活动单元格。我在宏运行时不小心点击工作表时遇到了问题,但是它会抛出错误。既然您已在条件中引用.Cells(iRow, #)
,为什么不使用.Cells(iRow, #).Row
?
其次,从你的条件想要做的事情来看,你似乎只记录一种类型的错误,即使对于给定的行有多个错误。这是你的意图吗?如果是这样,您可能需要考虑使用Else If语句,因此它只记录宏在数据表中的行遇到的第一个错误。
第三,你的lastARow参数没有改变,因为你没有告诉宏改变它。它在for循环之前设置,然后不被触摸。在写下错误后,您可能希望每次发现错误时增加lastARow。使用lastARow = lastARow + 1
最后,您可能会注意到最后一个If条件中的错误。我怀疑你想检查你的项目结束日期是否在2017年之前。