Excel 工作表(“查询”).Range(“H6”).AutoFill Destination:=工作表(“查询”).Range(“H6:H”和QlastRow) '移动到查询表 MsgBox“感谢您上传数据”&vbNewLine&&vbNewLine&“***事件
工作表(“查询”).Range(“H6”).AutoFill Destination:=工作表(“查询”).Range(“H6:H”和QlastRow) '移动到查询表 MsgBox“感谢您上传数据”&vbNewLine&&vbNewLine&“***事件任务***”&vbNewLine&“您现在将看到错过SLA的事件任务”&vbNewLine&“请提供理由或根据需要进行修改”、vbNewLine信息,“谢谢” 工作表(“查询”)。激活 '查询表的标题 工作表(“查询”).Range(“A4”).FormulaR1C1=“待审查的事件任务列表。” 工作表(“查询”)。范围(“A4:H4”)。选择 有选择 .HorizontalAlignment=xlCenter .垂直对齐=xl底部 .WrapText=False .方向=0 .AddIndent=False .1级别=0 .ShrinkToFit=False .ReadingOrder=xlContext .MergeCells=False 以 选择。合并 使用Selection.Font .Name=“Calibri” .尺寸=20 .Strikethrough=False .Superscript=False .Subscript=False .OutlineFont=False .Shadow=False .Underline=xlUnderlineStyleNone .ThemeColor=xlThemeColor灯1 .TintAndShade=0 .ThemeFont=xlThemeFontMinor 以 有选择。内饰 .Pattern=xlSolid .PatternColorIndex=xlAutomatic .Color=13532366 .TintAndShade=0 .PatternTintAndShade=0 以 '添加条件格式 “####此步骤有时在查询表的范围(“D1”)上执行 工作表(“查询”).Cells.FormatConditions.Delete 工作表(“查询”)。范围(“H6:H”和QlastRow)。选择 Selection.FormatConditions.Add类型:=xlExpression,公式1:=_ =和(F6=”“遗漏“”,H6“”) Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 带选择。格式条件(1)。内部 .PatternColorIndex=xlAutomatic .Color=RGB(0,176,80) .TintAndShade=0 以 Selection.FormatConditions.Add类型:=xlExpression,公式1:=_ “=和(F6=”未命中“、H6=”)” Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 带选择。格式条件(1)。内部 .PatternColorIndex=xlAutomatic .Color=RGB(255,0,0) .TintAndShade=0 以 Selection.FormatConditions.Add类型:=xlExpression,公式1:=_ =和(F6=”“met”“,H6”“) Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 带选择。格式条件(1)。内部 .PatternColorIndex=xlAutomatic .Color=RGB(0,176,80) .TintAndShade=0 以 Selection.FormatConditions.Add类型:=xlExpression,公式1:=_ =和(F6=“”符合“”,H6=“”) Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 带选择。格式条件(1)。内部 .PatternColorIndex=xlAutomatic .Color=RGB(198、89、17) .TintAndShade=0 以 Selection.FormatConditions(1).StopIfTrue=False 工作表(“查询”)。范围(“F6:F”和QlastRow)。选择 Selection.FormatConditions.Add类型:=xlExpression,公式1:=_ =和(F6=”“met”“,H6”“) Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 带选择。格式条件(1)。内部 .PatternColorIndex=xlAutomatic .Color=RGB(255,192,0) .TintAndShade=0 以 Selection.FormatConditions.Add类型:=xlExpression,公式1:=_ =和(F6=“”符合“”,H6=“”) Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 带选择。格式条件(1)。内部 .PatternColorIndex=xlAutomatic .Color=RGB(198、89、17) .TintAndShade=0 以 '现在显示宏已运行的结果 工作表(“查询”)。范围(“H6”)。选择 Application.ScreenUpdating=True MsgBox“请检查每个任务并选择违反SLA的原因”,VBEquipment,“检查SLA之外的事件任务” 如果结束 端接头Excel 工作表(“查询”).Range(“H6”).AutoFill Destination:=工作表(“查询”).Range(“H6:H”和QlastRow) '移动到查询表 MsgBox“感谢您上传数据”&vbNewLine&&vbNewLine&“***事件,excel,vba,validation,Excel,Vba,Validation,工作表(“查询”).Range(“H6”).AutoFill Destination:=工作表(“查询”).Range(“H6:H”和QlastRow) '移动到查询表 MsgBox“感谢您上传数据”&vbNewLine&&vbNewLine&“***事件任务***”&vbNewLine&“您现在将看到错过SLA的事件任务”&vbNewLine&“请提供理由或根据需要进行修改”、vbNewLine信息,“谢谢” 工作表(“查询”)。激活 '查询表的标题 工作表(“查询”).Range(“A4”)
如何确定“跳过”了哪一行?它总是同一行还是不同?通常跳过同一行。在我的代码中,我添加了注释,显示哪些行是定期的,有时是跳过的。注释以“####开头以便于查看。我的第一个假设是,多个
.Select
语句至少是问题的一部分,您将从阅读中受益。如果要检查是否执行了某些行(行只是随机跳过),我建议插入断点和/或插入Debug.Print
语句,显示正在处理的变量值等。谢谢。我会尽量减少。如果可以,选择语句。我还将对Debug.Print
进行一些调查,我听说过,但没有详细探讨。我昨天尝试了断点,结果证明它们没有用。我真的很希望VBA能够阅读并执行每一行代码——你知道为什么它会跳过吗?VBA执行每一行代码,在某些情况下,但是某些代码行可能没有预期的效果或根本没有任何效果。您通常不需要任何。选择语句,完全删除它们应该是目标。如何确定哪一行被“跳过”?它总是同一行还是不同?通常跳过同一行。在我的代码中,我添加了注释,显示哪些行是定期的,有时是跳过的。注释以“####开头以便于查看。我的第一个假设是,多个.Select
语句至少是问题的一部分,您将从阅读中受益。如果您想检查是否执行了某些行(行只是随机跳过),我建议插入断点和/或插入Debug.Print
语句,以显示您正在执行的变量的值
Sub AnalyseDataButton()
Dim Month As String
Month = Worksheets("Home").Range("B1")
Dim HlastRow As Long
HlastRow = Worksheets("Home").Range("A" & Rows.Count).End(xlUp).Row
Dim IlastRow
IlastRow = Worksheets(Month).Range("A" & Rows.Count).End(xlUp).Row
Dim lastRow As Long
lastRow = Worksheets(Month).Range("K" & Rows.Count).End(xlUp).Row
Dim QlastRow As Long
QlastRow = Worksheets("Queries").Range("A" & Rows.Count).End(xlUp).Row
Worksheets("Home").Calculate
'Validating all data has been added
If (Worksheets("Home").Range("A15") = "" Or Worksheets("Home").Range("K15") = "" Or Worksheets("Home").Range("U15") = "") Then
MsgBox "Please ensure you have added all three reports", vbExclamation + vbOKOnly, "Unable to run reports"
Else
'Complete all actions before showing results
Application.ScreenUpdating = False
'Prepare the September sheet for data
Worksheets(Month).UsedRange.ClearContents
'Move the data from Home to September, then clear the data from Home
Worksheets("Home").Range("A15").Select
Worksheets("Home").Range("A15:AA" & HlastRow).Copy Destination:=Worksheets(Month).Range("A1")
'Add additional columns as needed
Worksheets(Month).Range("T1:W1").EntireColumn.Insert
'INCIDENTS
'Apply Header to Actual Elapsed
Worksheets(Month).Range("T1") = "ActualElapsed"
'Apply Formula to T2
Worksheets(Month).Range("T2") = "=ROUNDUP(VLOOKUP(K2,$A$2:$I" & IlastRow & ",6,FALSE)/86400,0)"
'Copy Formula down to last row
Worksheets(Month).Range("T2").AutoFill Destination:=Worksheets(Month).Range("T2:T" & lastRow)
'Apply Header to Actual Met
Worksheets(Month).Range("U1") = "ActualMet"
'Apply Formula to U2
Worksheets(Month).Range("U2") = "=IF(NETWORKDAYS(M2,R2,HOLIDAYS)-1+MOD(M2,1)-MOD(R2,1)>5,""missed"",""met"")"
'Copy Formula down to last row
Worksheets(Month).Range("U2").AutoFill Destination:=Worksheets(Month).Range("U2:U" & lastRow)
'Apply Header to Business Met
Worksheets(Month).Range("V1") = "BusinessMet"
'Apply Formula to V2
Worksheets(Month).Range("V2") = "=IF(VLOOKUP(K2,$A$2:$H$" & IlastRow & ",8,FALSE)>432000,""missed"",""met"")"
'Copy Formula down to last row
Worksheets(Month).Range("V2").AutoFill Destination:=Worksheets(Month).Range("V2:V" & lastRow)
'Remove any Wrapped text
Worksheets(Month).Cells.WrapText = False
'Add Justification header on the Month tab
Worksheets(Month).Range("W1").Value = "Justification"
'Determine the list of query items
Worksheets("Queries").UsedRange.Clear
Worksheets(Month).Calculate
Worksheets(Month).Range("$K$1:$V$" & lastRow).AutoFilter Field:=11, Criteria1:="=missed"
Worksheets(Month).Range("K1:M" & Cells(Rows.Count, "K").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy Worksheets("Queries").Range("A5")
'Add data validation to the ActualMet
'### THIS STEP IS REGULARLY MISSED
With Worksheets("Queries").Range("F6").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=Dates!$G$1:$G$2"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.InputMessage = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
'Add data validation to the ActualMet
'### THIS STEP IS REGULARLY MISSED
Worksheets("Queries").Range("F6").AutoFill Destination:=Worksheets("Queries").Range("F6:F" & QlastRow)
'Continue to move data to the Queries sheet
Worksheets(Month).Range("R1:R" & Cells(Rows.Count, "K").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy Worksheets("Queries").Range("D5")
Worksheets(Month).Range("T1:V" & Cells(Rows.Count, "K").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy Worksheets("Queries").Range("E5")
Worksheets("Queries").Cells.WrapText = False
Worksheets("Queries").Columns("A:I").EntireColumn.AutoFit
Worksheets(Month).AutoFilterMode = False
Worksheets("Queries").Range("H5") = "Reasons for breaching SLA"
'Add data validation to the Justification
'### THIS STEP IS SOMETIMES MISSED
'### WHEN THIS STEP IS MISSED, THE RESULTS SHOW THE MACRO STARTED ON RANGE("H5")
With Worksheets("Queries").Range("H6").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=Dates!$J$1:$J$5"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.InputMessage = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Worksheets("Queries").Range("H6").AutoFill Destination:=Worksheets("Queries").Range("H6:H" & QlastRow)
'Move to Queries sheet
MsgBox "Thank you for uploading data." & vbNewLine & "" & vbNewLine & "*** INCIDENT TASKS ***" & vbNewLine & "You will now be shown the Incident Tasks which missed SLA." & vbNewLine & "Please provide justification or make amendments as required.", vbInformation, "Thank You"
Worksheets("Queries").Activate
'Header for Queries sheet
Worksheets("Queries").Range("A4").FormulaR1C1 = "List of INCIDENT TASKS to be reviewed."
Worksheets("Queries").Range("A4:H4").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection.Font
.Name = "Calibri"
.Size = 20
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 13532366
.TintAndShade = 0
.PatternTintAndShade = 0
End With
'Add conditional formatting
'### THIS STEP IS SOMETIMES PERFORMED ON RANGE ("D1") OF THE QUERIES SHEET
Worksheets("Queries").Cells.FormatConditions.Delete
Worksheets("Queries").Range("H6:H" & QlastRow).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(F6=""missed"",H6<>"""")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(0, 176, 80)
.TintAndShade = 0
End With
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(F6=""missed"",H6="""")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(255, 0, 0)
.TintAndShade = 0
End With
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(F6=""met"",H6<>"""")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(0, 176, 80)
.TintAndShade = 0
End With
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(F6=""met"",H6="""")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(198, 89, 17)
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Worksheets("Queries").Range("F6:F" & QlastRow).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(F6=""met"",H6<>"""")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(255, 192, 0)
.TintAndShade = 0
End With
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(F6=""met"",H6="""")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(198, 89, 17)
.TintAndShade = 0
End With
'Show results now the macro has run
Worksheets("Queries").Range("H6").Select
Application.ScreenUpdating = True
MsgBox "Please review each task and select the reason for breaching SLA.", vbExclamation, "Review the Incident Tasks outside of SLA"
End If
End Sub