best place to put "on error resume next" in this macro

Kemidan2014

Board Regular
Joined
Apr 4, 2022
Messages
226
Office Version
  1. 365
Platform
  1. Windows
So with the help of this community I had put together a macro and COMPLETELY neglected the fact that is IS in the realm of possibility that for a supplier code there could be NO OPEN COMPLAINTS

So i ran into that situation to where it errored out because there was nothing to copy lol I believe my solution would be to put in "On Error Resume Next"? some where

or is there an alternative. I tried adding it right before ".Range("A2:AK...." and it was error free but it is there a way that if it errors i can just exit the With part all together?


VBA Code:
Sub Filter_MHP()
    Worksheets("Complaints").Unprotect Password:="Secret"
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet
    Set srcWS = Sheets("Complaints")
    Set desWS = Sheets("MH Portage")
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    desWS.UsedRange.Offset(1).ClearContents
    With srcWS
        .Range("AW2:AW" & LastRow).Formula = "=IF(AND(D2=""0102-2"",M2>=TODAY()-7),""true"",IF(AND(D2=""0101-7"",ISBLANK(M2)),""true"",""false""))"
        .Cells(1).CurrentRegion.AutoFilter 49, "true"
        .Range("A2:AK" & LastRow).SpecialCells(xlCellTypeVisible).Copy
        With desWS
            .Cells(.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Columns.AutoFit
        End With
        .Range("A1").AutoFilter
        .Columns("AW").Delete
    End With
    Application.ScreenUpdating = True
    Worksheets("Complaints").Protect Password:="Secret"
    ThisWorkbook.Save
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
How about
VBA Code:
Sub Filter_MHP()
    Worksheets("Complaints").Unprotect Password:="Secret"
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet
    Set srcWS = Sheets("Complaints")
    Set desWS = Sheets("MH Portage")
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    desWS.UsedRange.Offset(1).ClearContents
    With srcWS
        .Range("AW2:AW" & LastRow).Formula = "=IF(AND(D2=""0102-2"",M2>=TODAY()-7),""true"",IF(AND(D2=""0101-7"",ISBLANK(M2)),""true"",""false""))"
        .Cells(1).CurrentRegion.AutoFilter 49, "true"
        .AutoFilter.Range.Offset(1).Columns("A:AK").Copy
        With desWS
            .Cells(.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Columns.AutoFit
        End With
        .Range("A1").AutoFilter
        .Columns("AW").Delete
    End With
    Application.ScreenUpdating = True
    Worksheets("Complaints").Protect Password:="Secret"
    ThisWorkbook.Save
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,571
Messages
6,120,302
Members
448,954
Latest member
EmmeEnne1979

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