Hi All,
I have a macro that work fine run from the editor but seems to skip a step when assigned to button. the part it seems to skip is
Full code is below. Anyone have any idea why it works from VBA edotior and not a button?
Thanks!!
I have a macro that work fine run from the editor but seems to skip a step when assigned to button. the part it seems to skip is
Code:
LRC = Sheets("Changes").Cells(Rows.Count, 1).End(xlUp).Row
If Count2 > 0 Then
Range("$A$1:$P$" & LRN).AutoFilter Field:=16, Criteria1:="YES"
Range("A2:O2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Changes").Select
Range("A" & LRC + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
LRC = Sheets("Changes").Cells(Rows.Count, 1).End(xlUp).Row
Else
End If
Full code is below. Anyone have any idea why it works from VBA edotior and not a button?
Thanks!!
Code:
Option Explicit
Sub CompareAndReport()
'
' Compare and Report
Dim SaveAsDialog As Object
Dim MWB
Dim NWB
Dim ws As Worksheet
Dim LRN
Dim LRO
Dim LRC
Dim Count1
Dim Count2
Dim SavePath As String
Dim Count3
'Create new wprkbook with Import amd Pre-import tabs
'Application.ScreenUpdating = False
Sheets("Pre-import").Select
Set SaveAsDialog = Application.FileDialog(msoFileDialogSaveAs)
Set MWB = ThisWorkbook
Sheets(Array("Pre-import", "Import")).Copy
ActiveWorkbook.Activate
Set NWB = ActiveWorkbook
'Create changes worksheet, remove existing validation
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Changes"
For Each ws In Worksheets
ws.Cells.Validation.Delete
Next ws
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'Test to make sure that there are changes to report on "Pre-import" tab
Sheets("Pre-Import").Activate
LRN = Cells(Rows.Count, 1).End(xlUp).Row
Range("K1").FormulaR1C1 = "New Project"
Range("K3").FormulaR1C1 = "=IFERROR(IF(ISNUMBER(NUMBERVALUE(MATCH(RC4,Import!R1C4:R102C4,0))),""NO"",""YES""),""YES"")"
Range("L1").FormulaR1C1 = "Approval Status Change"
Range("L3").FormulaR1C1 = "=IF(RC11=""YES"",""NA"",IFERROR(IF(RC4="""","""",IF(INDEX(Import!R1C7:R102C7,MATCH(RC4,Import!R1C4:R102C4,0))=RC7,""NO"",INDEX(Import!R1C7:R102C7,MATCH(RC4,Import!R1C4:R102C4,0)))),""NO""))"
Range("M1").FormulaR1C1 = "Start Date Change"
Range("M3").FormulaR1C1 = "=IF(RC11=""YES"",""NA"",IFERROR(IF(RC4="""","""",IF(INDEX(Import!R1C9:R102C9,MATCH(RC4,Import!R1C4:R102C4,0))=RC9,""NO"",INDEX(Import!R1C9:R102C9,MATCH(RC4,Import!R1C4:R102C4,0)))),""NO""))"
Range("N1").FormulaR1C1 = "Finish Date Change"
Range("N3").FormulaR1C1 = "=IF(RC11=""YES"",""NA"",IFERROR(IF(RC4="""","""",IF(INDEX(Import!R1C10:R102C10,MATCH(RC4,Import!R1C4:R102C4,0))=RC10,""NO"",INDEX(Import!R1C10:R102C10,MATCH(RC4,Import!R1C4:R102C4,0)))),""NO""))"
Range("O1").FormulaR1C1 = "Completed/Removed Project"
Range("O3").FormulaR1C1 = "=IF(RC[-11]="""","""",""NO"")"
Range("P1").FormulaR1C1 = "Exception"
Range("P3").FormulaR1C1 = "=IF(OR(LEN(RC12)>2,LEN(RC13)>2,LEN(RC14)>2),""YES"",""NO"")"
Range("A1:O1").Copy
Sheets("Changes").Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheets("Changes").Range("A1").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("K3:P3").AutoFill Destination:=Range("K3:P" & LRN)
On Error Resume Next
Worksheets("Pre-import").AutoFilterMode = False
Columns("A:P").AutoFilter
On Error GoTo 0
Count1 = WorksheetFunction.CountIf(Range("K2:K5000"), "YES")
Count2 = WorksheetFunction.CountIf(Range("P3:P5000"), "YES")
'----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'Test to make sure that there are changes to report on "Import" tab
Sheets("Import").Activate
LRO = Cells(Rows.Count, 1).End(xlUp).Row
Range("K3").FormulaR1C1 = "NO"
Range("L3").FormulaR1C1 = "NO"
Range("M3").FormulaR1C1 = "NO"
Range("N3").FormulaR1C1 = "NO"
Range("O3").FormulaR1C1 = "=IFERROR(IF(RC[-11]="""","""",MATCH(RC[-11],'Pre-Import'!R1C4:R5000C4,0)),""YES"")"
Range("K3:O3").AutoFill Destination:=Range("K3:O" & LRO)
On Error Resume Next
Worksheets("Import").AutoFilterMode = False
Columns("A:P").AutoFilter
On Error GoTo 0
Count3 = WorksheetFunction.CountIf(Range("O2:O5000"), "YES")
'----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'If nothing to report, close new workbook and msg the user
If Count1 = 0 And Count2 = 0 And Count3 = 0 Then '1
Sheets("changes").Range("A2") = "NO CHANGES FOR THIS DAY"
MsgBox "There are no changes for this day", vbInformation
Else
'If something to report, populate, format, condition report
Sheets("Pre-import").Activate
LRC = Sheets("Changes").Cells(Rows.Count, 1).End(xlUp).Row
If Count1 > 0 Then
Range("$A$1:$P$" & LRN).AutoFilter Field:=11, Criteria1:="Yes"
Range("A2:O2").Select
Range(Selection, Selection.End(xlDown)).Copy
Sheets("Changes").Range("A" & LRC + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
ActiveSheet.ShowAllData
Else
End If
LRC = Sheets("Changes").Cells(Rows.Count, 1).End(xlUp).Row
If Count2 > 0 Then
Range("$A$1:$P$" & LRN).AutoFilter Field:=16, Criteria1:="YES"
Range("A2:O2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Changes").Select
Range("A" & LRC + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
LRC = Sheets("Changes").Cells(Rows.Count, 1).End(xlUp).Row
Else
End If
Sheets("Import").Select
If Count3 > 0 Then
Range("$A$1:$O$" & LRO).AutoFilter Field:=15, Criteria1:="YES"
Range("A2:O2").Select
Range(Selection, Selection.End(xlDown)).Copy
Sheets("Changes").Range("A" & LRC + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Else
End If
End If
On Error Resume Next
Sheets("Pre-import").Select
Sheets("Pre-import").ShowAllData
Sheets("Pre-import").Range("K:P").Delete
'Application.Goto Reference:=Range("a1"), Scroll:=True
Sheets("Import").Select
Sheets("Import").ShowAllData
Sheets("Import").Range("K:P").Delete
'Application.Goto Reference:=Range("a1"), Scroll:=True
On Error GoTo 0
'Formatting
Sheets("Changes").Select
LRC = Cells(Rows.Count, 1).End(xlUp).Row
Range("A1:P" & LRC).AutoFilter
'Center Text
Range("H:O,A:D").Select
With Selection
.HorizontalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A:A").Select
With Selection
.HorizontalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Cells.EntireColumn.AutoFit
'Color fill change section
Columns("K:O").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
'Format date columns
Range("M1,N1,I1,J1").EntireColumn.NumberFormat = "m/d/yyyy"
'Condition to highlight changes
Range("K2:O5000").FormatConditions.Add Type:=xlExpression, Formula1:="=LEN(K2)>2"
Range("K2:O5000").FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Range("K2:O5000").FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Range("K2:O5000").FormatConditions(1).StopIfTrue = False
Application.Goto Reference:=Range("A1"), Scroll:=True
'prompt user to save file
SavePath = MWB.Path
With SaveAsDialog
.InitialFileName = SavePath & "\" & "Giraffe Scaffold Shedule - Changes - " & Format(Date, "MM.DD.YYYY")
.Title = "Save File"
.InitialView = msoFileDialogViewList
If .Show = True Then
Application.DisplayAlerts = False
.Execute
Application.DisplayAlerts = True
End If
End With
'Application.ScreenUpdating = True
End Sub