Getting a "Run-time Error 91"

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?

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
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,224,518
Messages
6,179,245
Members
452,900
Latest member
LisaGo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top