+ Reply to Thread
Results 1 to 6 of 6

VBA to split data to worksheet/workbook , delete zero values, set print area

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-26-2016
    Location
    TX
    MS-Off Ver
    Excel 2016
    Posts
    100

    VBA to split data to worksheet/workbook , delete zero values, set print area

    I have a macro that currently works for me (after much help from others on another thread) 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. On Sheet 1, how can I delete any rows that have zero value on Column K?

    2. 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?

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

    4. 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.

    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

  2. #2
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: VBA to split data to worksheet/workbook , delete zero values, set print area

    I think I can help with # 1. Add the following code into your existing code to delete rows that contain 0
     Dim Firstrow As Long
        Dim Lastrow As Long
        Dim Lrow As Long
        
    
        With Sheets("Sheet1")
        .Select
        End With
    
        Firstrow = 2
        Lastrow = Cells(Rows.Count, "K").End(xlUp).Row
        For Lrow = Lastrow To Firstrow Step -1
    
        With Cells(Lrow, "K")
    
             If Not IsError(.Value) Then
    
             If .Value = 0 Then .EntireRow.Delete
        
        End If
         End With
       Next Lrow
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  3. #3
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    423

    Re: VBA to split data to worksheet/workbook , delete zero values, set print area

    This should help with questions 1 and 3:
    Public Sub DeleteRowsAndSave()
      Dim r As Long
      Dim sh As Worksheet
      Dim wb As Workbook
      
      Set sh = ThisWorkbook.Worksheets("Sheet1")
      
      For r = sh.UsedRange.Rows.Count To 1 Step -1
        If sh.Cells(r, 11).Value = 0 Then sh.Rows(r).Delete
      Next
      
      sh.Copy
      ActiveWorkbook.SaveAs "D:\Temp\Test.xlsx"
      ActiveWorkbook.Close
    End Sub
    Unrelated to you questions, let me suggest you avoid using 'Select' - it is almost never needed, and it really slows things down. As an example, you can replace
    ActiveSheet.Range("a1:W1").Select
     Selection.Copy
    with
    ActiveSheet.Range("a1:W1").Copy

  4. #4
    Forum Contributor
    Join Date
    01-26-2016
    Location
    TX
    MS-Off Ver
    Excel 2016
    Posts
    100

    Re: VBA to split data to worksheet/workbook , delete zero values, set print area

    gmr4evr1 - Thank you for that code, it worked! this solved request #1.

    mgs73 - that didn't seem to work. As far as the alphabetical order of the sheets being saved, I realized it's because of the sorting that I have, I only intended it to do so on the sheets that are created. I'm attempting to include this after the Next ws portion but I'm getting an error (Compile Error: Next without For). Please see below. How can I properly move the highlighted yellow code?


    Sub Invoice()
        Dim Firstrow As Long
        Dim Lastrow As Long
        Dim Lrow As Long
        
    
        With Sheets("Sheet1")
        .Select
        End With
    
        Firstrow = 2
        Lastrow = Cells(Rows.Count, "K").End(xlUp).Row
        For Lrow = Lastrow To Firstrow Step -1
    
        With Cells(Lrow, "K")
    
             If Not IsError(.Value) Then
    
             If .Value = 0 Then .EntireRow.Delete
        
        End If
         End With
    
       Next Lrow
    
    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
    
    
       Next ws
     ActiveSheet.Range("a1:W1").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
    
       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

  5. #5
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: VBA to split data to worksheet/workbook , delete zero values, set print area

    I'm glad that my post worked for you.
    I think your Next without For error might have to do with the Next ws. You have to have a For in order to use Next. in this case, I think you need a For ws = .......

  6. #6
    Forum Contributor
    Join Date
    01-26-2016
    Location
    TX
    MS-Off Ver
    Excel 2016
    Posts
    100

    Re: VBA to split data to worksheet/workbook , delete zero values, set print area

    gmr4evr1 - that didn't work but it's possible I did this incorrectly. Would you mind sharing your suggestion within the coding I previously provided?

    Thanks in advance.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro - Set Print Area for Changing Data Area
    By ksp in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-27-2013, 04:19 AM
  2. delete everything but print area
    By bodhi2.71828@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-19-2013, 02:26 PM
  3. Replies: 1
    Last Post: 02-12-2013, 12:16 PM
  4. Split raw data into several worksheet and save as new workbook filter by column name
    By dare2join in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-29-2012, 11:15 AM
  5. VBA. identify unique values, loop them, filter them in, set print area & print it
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-09-2012, 09:56 PM
  6. Split cells without changing overall width of print area?
    By Kenny Bones in forum Excel General
    Replies: 2
    Last Post: 09-27-2011, 04:34 AM
  7. Split Workbook with Many Sheets into a file for each Worksheet and delete all others
    By Please_Help in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-19-2010, 07:31 PM
  8. Replies: 8
    Last Post: 05-23-2007, 06:39 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1