+ Reply to Thread
Results 1 to 8 of 8

Variable Print Area Macro Based on Cell Value

Hybrid View

  1. #1
    Registered User
    Join Date
    10-14-2009
    Location
    Denver, CO
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Variable Print Area Macro Based on Cell Value

    I've now posted the workbook. Could anyone help me with a macro that could sort the data on the 'Cost Codes' worksheet so that any rows with $0 in column I or H would be below those rows with $ totals in them. I would then print only the items that have $ totals, and leave the rows with zeros outside the print area. If I could then maintain the rest of the organization as close as possible after sorting, that would be helpful. May have to number the rows in cells in order to achieve this.


    Please let me know if you have thoughts. I can't tell you how much the help would be appreciated.


    Thanks again,

    Clay

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Variable Print Area Macro Based on Cell Value

    Hello Clay,

    The attached workbook has a button on the "Cost Codes" sheet to print out the non zero totals. Each row is added to the worksheet "Printout Data" to build the printable data in contiguous rows. You will probably need to adjust your margins on the "Printout Data" sheet. Let me know if this is what you want.
    Sub PrintData()
    
      Dim Cell As Range
      Dim DstWks As Worksheet
      Dim NextRow As Long
      Dim Rng As Range
      Dim RngEnd As Range
      Dim SrcWks As Worksheet
      
        Set SrcWks = Worksheets("Cost Codes")
        Set DstWks = Worksheets("Printout Data")
        
          Set Rng = SrcWks.Range("B6")
          Set RngEnd = SrcWks.Cells(Rows.Count, Rng.Column).End(xlUp)
          Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, SrcWks.Range(Rng, RngEnd))
          Set Rng = Rng.Resize(ColumnSize:=8)
          
          DstWks.UsedRange.Clear
          
            For Each Cell In Rng.Columns(7).Cells
              If Val(Cell.Value) > 0 Or Val(Cell.Offset(0, 1)) > 0 Then
                 NextRow = NextRow + 1
                 Rng.Rows(Cell.Row - Rng.Row + 1).Copy
                 DstWks.Cells(NextRow, "A").PasteSpecial Paste:=xlPasteValues
                 DstWks.Cells(NextRow, "A").PasteSpecial Paste:=xlPasteFormats
              End If
           Next Cell
           
         DstWks.UsedRange.Columns.AutoFit
         DstWks.PrintOut
         
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    10-14-2009
    Location
    Denver, CO
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Variable Print Area Macro Based on Cell Value

    Leith -

    This is amazing man! A couple questions for you:

    - Is there a way to make the 'Print Out' sheet preserve column headings from 'Cost Codes' sheet when the button is clicked?
    - Is there a way to remove the automatic print function? I only want to do so because we have a lot of networked printers, and my staff will need to manually print that sheet.

    And finally, do you have any recommendations on how I can begin to learn about creating macros for myself? I have seen a few posts on here where people were taught quite a lot from stem to stern. I am pretty adept in Excel itself, but have very limited experience with VBA.

    I honestly cannot tell you how much I appreciate your help. This is great.


    Thank you again,

    Clay

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Variable Print Area Macro Based on Cell Value

    Hello Clay,

    I can add the headings to the "Printout Data" sheet. Which ones do you want from the "Cost Codes"?

    I'll remove the automatic print function for you. Did it print out okay?

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Variable Print Area Macro Based on Cell Value

    Hello Clay,

    I didn't forget about you. I have made the changes to the workbook as you requested. Here is the updated macro that has been added to the attached workbook.
    Sub PrintData()
    
      Dim Cell As Range
      Dim DstWks As Worksheet
      Dim NextRow As Long
      Dim Rng As Range
      Dim RngEnd As Range
      Dim SrcWks As Worksheet
      
        NextRow = 2
        Set SrcWks = Worksheets("Cost Codes")
        Set DstWks = Worksheets("Printout Data")
        
          Set Rng = SrcWks.Range("B6")
          Set RngEnd = SrcWks.Cells(Rows.Count, Rng.Column).End(xlUp)
          Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, SrcWks.Range(Rng, RngEnd))
          Set Rng = Rng.Resize(ColumnSize:=8)
          
          With DstWks.UsedRange
            If .Rows.Count > 1 Then
               .Offset(1, 0).Resize(RowSize:=.Rows.Count - 1).Clear
            End If
          End With
          
          Application.ScreenUpdating = False
            For Each Cell In Rng.Columns(7).Cells
              If Val(Cell.Value) > 0 Or Val(Cell.Offset(0, 1)) > 0 Then
                 Rng.Rows(Cell.Row - Rng.Row + 1).Copy
                 DstWks.Cells(NextRow, "A").PasteSpecial Paste:=xlPasteValues
                 DstWks.Cells(NextRow, "A").PasteSpecial Paste:=xlPasteFormats
                 NextRow = NextRow + 1
              End If
           Next Cell
         Application.ScreenUpdating = True
         
         DstWks.PageSetup.CenterHeader = "&B" & Worksheets("Estimate Sheet").Range("I1")
         DstWks.UsedRange.Columns.AutoFit
         
    End Sub
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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