VBA copy the specific range if filter return no record

alantse2010

New Member
Joined
Jun 9, 2018
Messages
31
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
Hi Everyone.

I am trying to use the VBA to filter the data then copy from one worksheet to new worksheet. However, how can I copy the specific range (i.e.: A100:E100) if no filter result return?
Code:
  Dim wSheetStart As Worksheet 
  Dim wsDest As Worksheet
  Set wSheetStart = ThisWorkbook.Sheets("ATA")
  Application.ScreenUpdating = False
  Set wsDest = ThisWorkbook.Sheets.Add(After:= _
   ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    wsDest.Name = "New report" & Date
  wSheetStart.Activate
   wSheetStart.AutoFilterMode = False
   
   ActiveSheet.Range("A6:AC6").AutoFilter Field:=1, Criteria1:=">=" & DateSerial(Year(Now - 3), Month(Now - 3), Day(Now - 3)), Operator:=xlAnd, Criteria2:="<=" & DateSerial(Year(Now - 3), Month(Now - 3), Day(Now - 3))
   Worksheets("ATA").Range("A6:N6").Select
      'Worksheets("ATA").Range(Selection, Selection.End(xlToRight)).Select
     Worksheets("ATA").Range(Selection, Selection.End(xlDown)).Select
     Selection.Copy
     wsDest.Activate
     wsDest.Range("A1").Value = "(" & Format(Date - 3, "dd-mm-yyyy") & ")"
     wsDest.Range("A2").Value = "ATA" & wsDest.Range("A1").Value
     'wsDest.Range("A2:N2").Merge
     wsDest.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial
     
     wSheetStart.Activate
     wSheetStart.AutoFilterMode = False
   
   ActiveSheet.Range("A6:AC6").AutoFilter Field:=1, Criteria1:=">=" & DateSerial(Year(Now - 2), Month(Now - 2), Day(Now - 2)), Operator:=xlAnd, Criteria2:="<=" & DateSerial(Year(Now - 2), Month(Now - 2), Day(Now - 2))
   Worksheets("ATA").Range("A6:N6").Select
      'Worksheets("ATA").Range(Selection, Selection.End(xlToRight)).Select
     Worksheets("ATA").Range(Selection, Selection.End(xlDown)).Select
     Selection.Copy
     wsDest.Activate
    
     wsDest.Range("A" & Rows.Count).End(xlUp).Offset(2).Value = "(" & Format(Date - 2, "dd-mm-yyyy") & ")"
     wsDest.Range("A" & Rows.Count).End(xlUp).Offset(1).Value = "ATA" & Format(Date - 2, "dd-mm-yyyy")
     wsDest.Range("A" & Rows.Count).End(xlUp).Offset(1).Select
     Selection.PasteSpecial

I want the result look like this:
https://imgur.com/a/YSOEX42



Thank you very much for your help.
 
Last edited:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,215,391
Messages
6,124,673
Members
449,179
Latest member
fcarfagna

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