If visible cells are empty, then skip to the next sort

snuffnchess

Board Regular
Joined
May 15, 2015
Messages
68
Office Version
  1. 365
Platform
  1. Windows
Hello Excel Wizards

I am needing help on two things.

First:
The below code is working to essentially copy data that would exist from filters to a new worksheet, and then save that worksheet. It may be clunky... but it works.
Right now, if there is no data that appears in the filtered range, a workbook is created with just the header row. (So there are 4 files created for each value in Frandata.range("A" & Y) - but in some cases there may only need to be 1 file created.)
What I am wanting to do, though, is make it so that if the filtered range does not show data, that it just moves on to the next set of data to filter.


Secondly:
Other than attaching a variable to the "ardata.Range(Cells(1, 1), Cells(arlrow, arlcol + 5))" range... is there anything else that I could do to optimize this part of the code?




VBA Code:
    For y = 1 To frandatalrow
        Set nb = Workbooks.Add
        Set ns = nb.Worksheets(1)
        ardata.Activate
            Range(Cells(1, 1), Cells(arlrow, arlcol + 5)).AutoFilter Field:=1, Criteria1:=frandata.Range("A" & y).Value
            ardata.Range(Cells(1, 1), Cells(arlrow, arlcol + 5)).AutoFilter Field:=11, Criteria1:="DA"
            ardata.Range(Cells(1, 1), Cells(arlrow, arlcol + 5)).AutoFilter Field:=9, Criteria1:="ccauto"
            ardata.Range(Cells(1, 2), Cells(arlrow, 8)).SpecialCells(xlCellTypeVisible).Copy
        ns.Cells(1, 1).PasteSpecial xlPasteAll
        ns.Columns.AutoFit
            sFilename = frandata.Range("A" & y).Value
            nb.SaveAs sfolca & "CC Auto\" & sFilename & ".xlsx", 51
        nb.Close False
        ardata.ShowAllData
        
        
        Set nb = Workbooks.Add
        Set ns = nb.Worksheets(1)
        ardata.Activate
            Range(Cells(1, 1), Cells(arlrow, arlcol + 5)).AutoFilter Field:=1, Criteria1:=frandata.Range("A" & y).Value
            ardata.Range(Cells(1, 1), Cells(arlrow, arlcol + 5)).AutoFilter Field:=11, Criteria1:="DA"
            ardata.Range(Cells(1, 1), Cells(arlrow, arlcol + 5)).AutoFilter Field:=9, Criteria1:="ccman"
            ardata.Range(Cells(1, 2), Cells(arlrow, 8)).SpecialCells(xlCellTypeVisible).Copy
        ns.Cells(1, 1).PasteSpecial xlPasteAll
        ns.Columns.AutoFit
            sFilename = frandata.Range("A" & y).Value
            nb.SaveAs sfolca & "CC Manual\" & sFilename & ".xlsx", 51
        nb.Close False
        ardata.ShowAllData
            
            
        Set nb = Workbooks.Add
        Set ns = nb.Worksheets(1)
        ardata.Activate
            ardata.Range(Cells(1, 1), Cells(arlrow, arlcol + 5)).AutoFilter Field:=1, Criteria1:=frandata.Range("A" & y).Value
            ardata.Range(Cells(1, 1), Cells(arlrow, arlcol + 5)).AutoFilter Field:=11, Criteria1:="HE"
            ardata.Range(Cells(1, 1), Cells(arlrow, arlcol + 5)).AutoFilter Field:=9, Criteria1:="achauto"
            ardata.Range(Cells(1, 2), Cells(arlrow, 8)).SpecialCells(xlCellTypeVisible).Copy
        ns.Cells(1, 1).PasteSpecial xlPasteAll
        ns.Columns.AutoFit
            sFilename = frandata.Range("A" & y).Value
            nb.SaveAs sfolca & "ACH Manual\" & sFilename & ".xlsx", 51
        nb.Close False
        ardata.ShowAllData
        
        
        
        Set nb = Workbooks.Add
        Set ns = nb.Worksheets(1)
        ardata.Activate
            ardata.Range(Cells(1, 1), Cells(arlrow, arlcol + 5)).AutoFilter Field:=1, Criteria1:=frandata.Range("A" & y).Value
            ardata.Range(Cells(1, 1), Cells(arlrow, arlcol + 5)).AutoFilter Field:=11, Criteria1:="HE"
            ardata.Range(Cells(1, 1), Cells(arlrow, arlcol + 5)).AutoFilter Field:=9, Criteria1:="achman"
            ardata.Range(Cells(1, 2), Cells(arlrow, 8)).SpecialCells(xlCellTypeVisible).Copy
        ns.Cells(1, 1).PasteSpecial xlPasteAll
        ns.Columns.AutoFit
            sFilename = frandata.Range("A" & y).Value
            nb.SaveAs sfolca & "ACH Manual\" & sFilename & ".xlsx", 51
        nb.Close False
        ardata.ShowAllData
    
    
    Next y
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Well folks... nevermind I think i figured out my own answer.

Just in case anybody else is looking for how to do this, I added in a

If / Then for

If ardata.Range(Cells(1, 2), Cells(arlrow, 8)).SpecialCells(xlCellTypeVisible).Rows.Count > 1 Or ardata.Range(Cells(1, 2), Cells(arlrow, 8)).SpecialCells(xlCellTypeVisible).Areas.Count > 1 Then

Proceed on.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,678
Members
449,116
Latest member
HypnoFant

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