Abord sub if criteria is not met

AlexSrois

New Member
Joined
Aug 14, 2021
Messages
18
Office Version
  1. 2016
Platform
  1. Windows
Hi guys! Need your help!

I've got a macro that :
1) Select all the rows from a table when the A value is ''Complété''
2) Copy these rows to another worksheet
3) Delete theses rows from the first table

My sub is working perfectly, execepted that if there is no value that meets the criteria (''complété''), then it deletes everything. I'm pretty sure you can see why this is a problem ;)

How can I make the sub to abord itself if the criteria isn't met?

Here is my sub ;
VBA Code:
Sub Archive()

' Copy Completed task value from WorksheetPanier to WorksheetArchives

Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet

    Sheets("Panier").Select
   ActiveSheet.ListObjects("Panier").Range.AutoFilter Field:=1, Criteria1:= _
        "Complété"
    Range("Panier").Select
    Selection.Copy
  
  Set copySheet = Worksheets("Panier")
  Set pasteSheet = Worksheets("Archives")

  pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
  
' Go back and delete Completed task
  
    Sheets("Panier").Select
    Application.CutCopyMode = False
    Selection.EntireRow.Delete
    ActiveSheet.ListObjects("Panier").Range.AutoFilter Field:=1
     
' End routine

    ThisWorkbook.Save
    ActiveWorkbook.RefreshAll
    
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
How about if you add this one line of code where indicated:

VBA Code:
Sub Archive()

' Copy Completed task value from WorksheetPanier to WorksheetArchives

Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet

    Sheets("Panier").Select
   ActiveSheet.ListObjects("Panier").Range.AutoFilter Field:=1, Criteria1:= _
        "Complété"
    Range("Panier").Select
    If Selection.Offset(1, 0).SpecialCells(xlCellTypeVisible).Cells.Count) = ActiveSheet.ListObjects("Panier").ListColumns.Count Then Exit Sub '**** ADD THIS LINE ***
    Selection.Copy
  
  Set copySheet = Worksheets("Panier")
  Set pasteSheet = Worksheets("Archives")

  pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
  
' Go back and delete Completed task
  
    Sheets("Panier").Select
    Application.CutCopyMode = False
    Selection.EntireRow.Delete
    ActiveSheet.ListObjects("Panier").Range.AutoFilter Field:=1
     
' End routine

    ThisWorkbook.Save
    ActiveWorkbook.RefreshAll
    
End Sub
 
Upvote 0
Solution
How about if you add this one line of code where indicated:

VBA Code:
Sub Archive()

' Copy Completed task value from WorksheetPanier to WorksheetArchives

Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet

    Sheets("Panier").Select
   ActiveSheet.ListObjects("Panier").Range.AutoFilter Field:=1, Criteria1:= _
        "Complété"
    Range("Panier").Select
    If Selection.Offset(1, 0).SpecialCells(xlCellTypeVisible).Cells.Count) = ActiveSheet.ListObjects("Panier").ListColumns.Count Then Exit Sub '**** ADD THIS LINE ***
    Selection.Copy
 
  Set copySheet = Worksheets("Panier")
  Set pasteSheet = Worksheets("Archives")

  pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
 
' Go back and delete Completed task
 
    Sheets("Panier").Select
    Application.CutCopyMode = False
    Selection.EntireRow.Delete
    ActiveSheet.ListObjects("Panier").Range.AutoFilter Field:=1
   
' End routine

    ThisWorkbook.Save
    ActiveWorkbook.RefreshAll
  
End Sub
Thought it was working, but that doesn't copy then erase anymore ; it just prevent from error delete
 
Last edited:
Upvote 0
You're welcome. I was happy to help. Thanks for the feedback.
 
Upvote 0
Thought it was working, but that doesn't copy then erase anymore ; it just prevent from error delete
Fixed! Added an "Else" at the end.
Thank you so much for your formula.
Anyway I can erase the filter before exiting sub?
 
Upvote 0
In the appropriate spot in your code try this...

VBA Code:
 ActiveSheet.ListObjects("Panier").AutoFilter.ShowAllData
 
Upvote 0
In the appropriate spot in your code try this...

VBA Code:
 ActiveSheet.ListObjects("Panier").AutoFilter.ShowAllData
I mean, after the If statement ;
Like
VBA Code:
If Selection.Offset(1, 0).SpecialCells(xlCellTypeVisible).Cells.Count = ActiveSheet.ListObjects("Panier").ListColumns.Count Then ActiveSheet.ListObjects("Panier").AutoFilter.ShowAllData Then Exit Sub Else
    Selection.Copy
That would not work, but you get what I'm trying to do ;)
 
Upvote 0
You would need to make it an if/then multiple lines - Perhaps this...

VBA Code:
If Selection.Offset(1, 0).SpecialCells(xlCellTypeVisible).Cells.Count = ActiveSheet.ListObjects("Panier").ListColumns.Count Then
     ActiveSheet.ListObjects("Panier").AutoFilter.ShowAllData
     Exit Sub
End if
 
Upvote 0
I did not account for the fact that you want to the sub to do other stuff, but I think you had that figured out already....
 
Upvote 0
You would need to make it an if/then multiple lines - Perhaps this...

VBA Code:
If Selection.Offset(1, 0).SpecialCells(xlCellTypeVisible).Cells.Count = ActiveSheet.ListObjects("Panier").ListColumns.Count Then
     ActiveSheet.ListObjects("Panier").AutoFilter.ShowAllData
     Exit Sub
End if
Great, everything seems to be working fine! Thank a lot for your time and patience! :D
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,050
Members
449,206
Latest member
Healthydogs

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