VBA split data as worksheets alphabetically, save tabs as workbooks, set print area.

TxRookie

New Member
Joined
Apr 24, 2017
Messages
1
I have a macro that currently works for me (after much help from others in another forum) but I would like some help tweaking it.
Please note that the subtotal function excludes the first sheet because doing so isn't necessary and will cause excel to crash due to #of rows.



1. The data is split into worksheets by column A (Invoice #) but it isn't done in alphabetical order, even if the first sheet is in alphabetical order. How can I change that?

2. How can I save each tab as a separate Excel 2013 file without deleting it in the original sheet?

3. I have a print area set that includes all columns but I would like to auto detect how many pages are needed. The limit of rows I would like to have in one page is 75. For ex, an invoice with 230+ rows would require 4 sheets.

Sample Data
InvoiceDescrCustomerAcctg DateAP UnitVoucherLineShip ToUlt Use CodeCodeVendorNameInvoiceGL UnitEntityAccountDeptProjectActivitySource TypeStTaxablePO No.PO Unit Amount Tax Percent ID Source Descr
ABC-0000001357TX0000000000007973/23/2017XXXXX004976491TX32578412345ABCDEFG7890009999AB000056FGHIJ987654NYE20000XXXXX12345 3,962.00 - - Non POVchr
ABC-0000001360CA0000000000008213/29/2017XXXXX004988281CA100909112345ABCDEFG7890009999AB000060FGHIJ987654CAE20000XXXXX12345 3,300.00 - - PO Vchr
ABC-0000001361CA0000000000008503/31/2017XXXXX004993441CA32579412345ABCDEFG7890009999AB000048FGHIJ987654CAE20000XXXXX12345 3,962.00 - - PO Vchr
ABC-0000001362TX0000000000021133/22/2017XXXXX0049742012TX9318612345ABCDEFG7895009999YZ001932FGHIJ987654TXT 174.25 - - Non POVchr
ABC-0000001369AL0000000000004753/21/2017XXXXX004974031AL1702512345ABCDEFG7890009999AB000024FGHIJ987654ALE20000XXXXX12345 5,141.01 - - PO Vchr
ABC-0000001369AL0000000000004753/21/2017XXXXX004974032AL1702512345ABCDEFG7890009999AB000024FGHIJ987654ALE20000XXXXX12345 752.40 - - PO Vchr
ABC-0000001369AL0000000000004753/21/2017XXXXX004974033AL1702512345ABCDEFG7890009999AB000024FGHIJ987654ALE20000XXXXX12345 1,777.23 - - PO Vchr
ABC-0000001369AL0000000000004753/21/2017XXXXX004974034AL1702512345ABCDEFG7890009999AB000024FGHIJ987654ALE20000XXXXX12345 2,322.10 - - PO Vchr
ABC-0000001369AL0000000000004753/21/2017XXXXX004974032AL1702512345ABCDEFG7890009999AB000024FGHIJ987658ALE20000XXXXX12345 752.40 - - PO Vchr
ABC-0000001369AL0000000000004753/21/2017XXXXX004974033AL1702512345ABCDEFG7890009999AB000024FGHIJ987658ALE20000XXXXX12345 1,777.23 - - PO Vchr
ABC-0000001369AL0000000000004753/21/2017XXXXX004974034AL1702512345ABCDEFG7890009999AB000024FGHIJ987658ALE20000XXXXX12345 2,322.10 - - PO Vchr
XYZ-0000013783TX0000000000020143/24/2017XXXXX004979081TXI1701712345ABCDEFG7895009999YZ002596FGHIJ987654PAT20000XXXXX12345 8,712.00 522.72 6.00 PO Vchr
XYZ-0000013783TX0000000000020143/24/2017XXXXX004979082TXI1701712345ABCDEFG7895009999YZ002596FGHIJ987654PAT20000XXXXX12345 8,712.00 522.72 6.00 PO Vchr
XYZ-0000013783TX0000000000020143/24/2017XXXXX004979083TXI1701712345ABCDEFG7895009999YZ002596FGHIJ987654PAE20000XXXXX12345 2,178.00 - - PO Vchr
XYZ-0000013783TX0000000000020143/24/2017XXXXX004979084TXI1701712345ABCDEFG7895009999YZ002596FGHIJ987654PAE20000XXXXX12345 2,178.00 - - PO Vchr
XYZ-0000013784TX0000000000020143/30/2017XXXXX0049898056TX9329012345ABCDEFG7895009999YZ002597FGHIJ987654PAE 44.17 - - Non POVchr
XYZ-0000013784AL0000000000020143/21/2017ZZZZZ012345ABCDEFG7895009999YZ002597FGHIJ987654PAE 1,200.00 - - INV

<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>



Thanks in advance for your help.




Code:
Sub Invoice()
 
 
Dim ws As Worksheet, a, e, dic As Object
    
 
      Cells.Select
              With Selection.Font
                    .Name = "Arial Narrow"
                    .Size = 10
                    Rows("1:10000").RowHeight = 15
                    Columns("K:L").Select
                    Selection.Style = "Comma"
                               
End With
 
 ActiveSheet.Range("a1:W1").Select
 Selection.Copy
 On Error Resume Next
 Application.ScreenUpdating = False
 For Each ws In Worksheets
 ws.Columns("A:W").Sort Key1:=ws.Columns("J"), Order1:=xlDescending, Key2:=ws.Columns("O"),   Order2:=xlAscending
   Next ws
   ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteAll
   Application.ScreenUpdating = True
 
    Application.ScreenUpdating = False
    Set dic = CreateObject("Scripting.Dictionary")
    With Sheets("sheet1").Cells(1).CurrentRegion
        .Parent.AutoFilterMode = False
        a = .Columns(1).Offset(1).Resize(.Rows.Count - 1).Value
        For Each e In a
            If Not dic.exists(e) Then
                dic(e) = Empty
                If Not Evaluate("isref('" & e & "'!a1)") Then
                    Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = e
                End If
                Sheets(e).Cells.Clear
                .AutoFilter 1, e
                .Copy Sheets(e).Cells(1)
                With Sheets(e).Cells(1).CurrentRegion
                  
 .Subtotal GroupBy:=10, Function:=xlSum, TotalList:=Array(11, 12)
               .Parent.Cells.ClearOutline
                .Columns.AutoFit
                .PageSetup.PrintTitleRows = "$1:$1"
If .Rows.Count > 75 Then
                For i = 76 To .Rows.Count Step 75
                .Parent.HPageBreaks.Add before:=.Rows(i)
                        Next
                End If
                                With .Parent.PageSetup
                                .Orientation = xlLandscape
                                .FitToPagesWide = 1  '? ---- changed
                                .Zoom = False
End With
                                 .AutoFilter
        
                End With
                                 Application.ScreenUpdating = True
 
                End If
 Next
                End With
 
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,216,174
Messages
6,129,297
Members
449,499
Latest member
HockeyBoi

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