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

Kemidan2014

Board Regular
Joined
Apr 4, 2022
Messages
84
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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
77,590
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

Forum statistics

Threads
1,175,751
Messages
5,899,274
Members
434,759
Latest member
carcharoth

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
Top