figgylynn1023
New Member
- Joined
- Jul 21, 2011
- Messages
- 24
I am trying to create a macro that will help compare today's data sets with the data sets from the previous day. I want the macro to:
1. Open the previous day workbook, clear fields and sort column F in ascending order on the worksheet "Report Comparison" in that workbook.
2. Go back to the current day workbook, clear fields and sort column F in ascending order on the worksheet "Report Comparison" in that workbook.
3. Run a duplicates formula that will spit back "PDD" for previous day duplicates and "SDD" for Same Day Duplicates on the Current day worksheet (to be posted in column C on the "Report Comparison" worksheet of the current day wookbook).
However, with the code I have below, when I try to run the macro, it stops and give me the message "Run-time error '91': Object variable or With block variable not set'. (Where I've made the text red). I'm confused because I copied this code from another report that it worked for. Thoughts?
1. Open the previous day workbook, clear fields and sort column F in ascending order on the worksheet "Report Comparison" in that workbook.
2. Go back to the current day workbook, clear fields and sort column F in ascending order on the worksheet "Report Comparison" in that workbook.
3. Run a duplicates formula that will spit back "PDD" for previous day duplicates and "SDD" for Same Day Duplicates on the Current day worksheet (to be posted in column C on the "Report Comparison" worksheet of the current day wookbook).
However, with the code I have below, when I try to run the macro, it stops and give me the message "Run-time error '91': Object variable or With block variable not set'. (Where I've made the text red). I'm confused because I copied this code from another report that it worked for. Thoughts?
Rich (BB code):
Sub Duplicates()
'
' Duplicates Macro
'
Dim Day As String
Dim Prev As String
Day = InputBox("Today's File(mmddyy):")
Prev = InputBox("Previous Date(mmddyy):")
' Opening Previous Day
Workbooks.Open Filename:= _
"\\Drive\Folder\Reports\Report_Comparison_" & Prev & "_All.xlsm"
Windows("Report_Comparison_" & Prev & "_All.xlsm").Activate
Selection.AutoFilter
ActiveWorkbook.Worksheets("Report Comparison").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Report Comparison").AutoFilter.Sort.SortFields.Add _
Key:=Range("F1:F65536"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Report Comparison").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' Back to Current Day
Windows("Report_Comparison_" & Day & "_All.xlsm").Activate
Selection.AutoFilter
ActiveWorkbook.Worksheets("Report Comparison").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Report Comparison").AutoFilter.Sort.SortFields.Add _
Key:=Range("F1:F65536"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Report Comparison").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' Duplicates Formula
Columns("A:C").Select
Selection.NumberFormat = "General"
Range("C2").FormulaR1C1 = _
"=IFERROR(IF(VLOOKUP(RC[3],'[Report_Comparison_" & Prev & "_All.xlsm]Report Comparison'!C6,1,FALSE)=RC[3],""PDD"",""""),IF(RC[3]=R[1]C[3],""SDD"",""""))"
Range("D2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub