+ Reply to Thread
Results 1 to 17 of 17

creating macro to print all columns but only rows with data

Hybrid View

  1. #1
    Registered User
    Join Date
    04-06-2016
    Location
    pennsylvania
    MS-Off Ver
    excel 2010
    Posts
    9

    Question creating macro to print all columns but only rows with data

    Hello all,


    I've created a template in excel to calculate various results (mean, standard deviation, etc.) I have column A dedicated to numbering (1-100); B-O for users to enter their own information, columns P-W have various calculation formulas. Rows 12-111 are where data (analyte names and associated values for calculations) will be entered. My co-workers will use this, so I want this to automatically print the appropriate selection without them having to do a print selection. They may have anywhere from 1-100 analytes to calculate. The Sheet name is MDL Calc Form.

    I always want all columns to print and I always want the first 11 rows to print, but I only want the rows from 12:111 to print if data is entered in merged column B:C (Analyte Name). Unfortunately for me, since I have formulas extending down to 111, the macro I was trying to use reads that as a value and I cannot figure out a way around this.

    So, always print area: A1-A11:W1-W11
    Only print A12-W12 if BC12 has value
    Only print A13-W13 if BC13 has value, etc. etc.

    Also, I think I can save the workbook to automatically print 'fit all columns on one page' so it always prints that way, but in case I am wrong please include that also. I couldn't figure out a way to attach the excel file either.

    I would be forever grateful to anyone who could write me a macro to do this. I am utterly lost and have little coding knowledge. Please help!

    Thank you!!
    jkarns

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: creating macro to print all columns but only rows with data

    To attach a file: Just press 'Go Advanced' button below and choose Manage attachments, it's simple, follow the instructions.
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Registered User
    Join Date
    04-06-2016
    Location
    pennsylvania
    MS-Off Ver
    excel 2010
    Posts
    9

    Re: creating macro to print all columns but only rows with data

    [attach]xlsm.gif DRAFT L40.20 r0 MDL Calculation Form with Summary.xlsm[/attach]

    Let's try this maybe? Well that's one problem down! Thanks Keebellah!

  4. #4
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: creating macro to print all columns but only rows with data

    Okay, got the file, yo should at least take into account that a Division by 0 is not allowed. A sheet full of #DIV/0! that's 'not-done'

  5. #5
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: creating macro to print all columns but only rows with data

    Okay, try this one
    There is a small Printer Icon on the MDL form sheet

  6. #6
    Registered User
    Join Date
    04-06-2016
    Location
    pennsylvania
    MS-Off Ver
    excel 2010
    Posts
    9

    Re: creating macro to print all columns but only rows with data

    So it still printed all the rows. Is it possible to make the macro read if a name is typed into column BC, row 12,13,14 etc. it will only print that row? So in other words, if there are only two analytes entered, I only want it to print those two rows (plus the first 11 rows.) Any suggestions?

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: creating macro to print all columns but only rows with data

    If cell B is empty that row will NOT be printed.
    I tested it and it works, so you'll have to check what you've done.
    Did you copy my code to another file? Then you forgot the code in the Sheet.

    I though you wanted to print the top 11 rows in the table, all you have to do is modify the macro:
    Public Function Prepare_MDL_Calc_Form()
    '
    ' PrintMDL_Calc_Form Macro
    '
    If ActiveSheet.Name <> "MDL Calc Form" Then Exit Function
    Application.ScreenUpdating = False
    Dim ws      As Worksheet
    Dim xrow    As Long
    Dim lstRow  As Long
    Set ws = Worksheets("MDL Calc Form")
    lstRow = 111
    Application.Calculation = xlCalculationManual
    For xrow = lstRow To 23 Step -1
        If Len(Trim(ws.Cells(xrow, "B").Value)) = 0 Then ws.Cells(xrow, "B").EntireRow.Hidden = True
    Next xrow
    Application.Calculation = xlCalculationAutomatic
    Change 23 to 12 which is the first row of your table
    Last edited by Keebellah; 04-07-2016 at 09:05 AM.

  8. #8
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: creating macro to print all columns but only rows with data

    Apart from that I think you should do it as follows:

    Check this code and add the line in BOLD RED

    Public Sub Print_MDL_Calc_Form()
    If MsgBox("Print MDL SUMMARY FORM?", vbQuestion + vbYesNo, "") <> vbYes Then Exit Sub
    Worksheets("MDL Calc Form").Select
    Prepare_MDL_Calc_Form
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
    resetHiddenRows
    End Sub
    You can remove the code in Thiswokbook
    Private Sub Workbbok_BeforePrint etc
    
    End Sub
    

  9. #9
    Registered User
    Join Date
    04-06-2016
    Location
    pennsylvania
    MS-Off Ver
    excel 2010
    Posts
    9

    Re: creating macro to print all columns but only rows with data

    Awesome! It worked on hiding all the cells! The problem is, I don't want to run the macro until the data has been entered and ready to print. So type in all data, save as (whatever data this corresponds to), then when whoever goes to print, I want the macro to run. I had to copy the macro into the original file I had. I attached a copy of it in the thread so I don't have the printer icon. Everything else works, but the macro starts as soon as the file is opened. Can this be avoided?

  10. #10
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: creating macro to print all columns but only rows with data

    Open both files, then just select the printer icon from the file I uploaded, got to your file and on that page select where you want it and select Paste.
    Then all you have to do is reassign the macro to the one in your working file.
    The rows will be hidden only when the print command is given (the macro) and then the rows will become visible again.
    Last edited by Keebellah; 04-07-2016 at 09:40 AM. Reason: Info

  11. #11
    Registered User
    Join Date
    04-06-2016
    Location
    pennsylvania
    MS-Off Ver
    excel 2010
    Posts
    9

    Re: creating macro to print all columns but only rows with data

    My problem has been solved. Thank you Keebellah for all of your help. I am very grateful!!

  12. #12
    Registered User
    Join Date
    04-06-2016
    Location
    pennsylvania
    MS-Off Ver
    excel 2010
    Posts
    9

    Re: creating macro to print all columns but only rows with data

    I'm getting an error Worksheets ("MDL Calc Form").Select line

    I added the Prepare_MDL_Calc_Form

  13. #13
    Registered User
    Join Date
    04-06-2016
    Location
    pennsylvania
    MS-Off Ver
    excel 2010
    Posts
    9

    Re: creating macro to print all columns but only rows with data

    I get Run-Time error '9': Subscript out of range when I hit the print button. It worked on the original sheet, just not when I copied it.

  14. #14
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: creating macro to print all columns but only rows with data

    You're welcome.

  15. #15
    Registered User
    Join Date
    04-06-2016
    Location
    pennsylvania
    MS-Off Ver
    excel 2010
    Posts
    9

    Re: creating macro to print all columns but only rows with data

    Keebellah, I am having issues again. Once I copied the macro it was linking it to the workbook from here (which now for some reason it can't find.) Could you copy and paste the macro into a thread so I can create a new one and copy it from here? The excel file is linked and I feel like all of this is starting to melt my brain...

    Please help.
    Last edited by jkarns; 04-07-2016 at 10:35 AM.

  16. #16
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: creating macro to print all columns but only rows with data

    The red macro is the one that should be linked to the print command or icon

    Option Explicit
    
    Public Sub Print_MDL_Calc_Form()
    If MsgBox("Print MDL SUMMARY FORM?", vbQuestion + vbYesNo, "") <> vbYes Then Exit Sub
    Worksheets("MDL Calc Form").Select
    Prepare_MDL_Calc_Form
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
    resetHiddenRows
    End Sub
    
    Public Function resetHiddenRows()
    Application.ScreenUpdating = False
    Dim ws      As Worksheet
    Dim xrow    As Long
    Dim lstRow  As Long
    Set ws = Worksheets("MDL Calc Form")
    ws.Range(Cells(12, "A"), Cells(100, "A")).EntireRow.Hidden = False
    Application.ScreenUpdating = True
    End Function
    
    Public Function Prepare_MDL_Calc_Form()
    '
    ' PrintMDL_Calc_Form Macro
    '
    If ActiveSheet.Name <> "MDL Calc Form" Then Exit Function
    Application.ScreenUpdating = False
    Dim ws      As Worksheet
    Dim xrow    As Long
    Dim lstRow  As Long
    Set ws = Worksheets("MDL Calc Form")
    lstRow = 111
    Application.Calculation = xlCalculationManual
    For xrow = lstRow To 12 Step -1
        If Len(Trim(ws.Cells(xrow, "B").Value)) = 0 Then ws.Cells(xrow, "B").EntireRow.Hidden = True
    Next xrow
    Application.Calculation = xlCalculationAutomatic
    
        Application.PrintCommunication = False
        With ActiveSheet.PageSetup
            .PrintTitleRows = ""
            .PrintTitleColumns = ""
        End With
        Application.PrintCommunication = True
        ActiveSheet.PageSetup.PrintArea = "$A$1:$W$111"
        Application.PrintCommunication = False
        With ActiveSheet.PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = ""
            .LeftFooter = "FOR MRLLC USE ONLY"
            .CenterFooter = ""
            .RightFooter = _
            "Mean Recovery %: Red font indicates failure, acceptable range 70-130%" & Chr(10) & "Column A: MDL > 1/10 concentration of spike, * indicates failure" & Chr(10) & "Column B: MDL < spike concentrataion, * indicates failure" & Chr(10) & "&P"
            .LeftMargin = Application.InchesToPoints(0.236220472440945)
            .RightMargin = Application.InchesToPoints(0.236220472440945)
            .TopMargin = Application.InchesToPoints(0.748031496062992)
            .BottomMargin = Application.InchesToPoints(0.94488188976378)
            .HeaderMargin = Application.InchesToPoints(0.31496062992126)
            .FooterMargin = Application.InchesToPoints(0.31496062992126)
            .PrintHeadings = False
            .PrintGridlines = False
            .PrintComments = xlPrintNoComments
            .CenterHorizontally = False
            .CenterVertically = False
            .Orientation = xlLandscape
            .Draft = False
            .PaperSize = xlPaperLetter
            .FirstPageNumber = xlAutomatic
            .Order = xlDownThenOver
            .BlackAndWhite = False
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = False
            .PrintErrors = xlPrintErrorsDisplayed
            .OddAndEvenPagesHeaderFooter = False
            .DifferentFirstPageHeaderFooter = False
            .ScaleWithDocHeaderFooter = True
            .AlignMarginsHeaderFooter = True
            .EvenPage.LeftHeader.Text = ""
            .EvenPage.CenterHeader.Text = ""
            .EvenPage.RightHeader.Text = ""
            .EvenPage.LeftFooter.Text = ""
            .EvenPage.CenterFooter.Text = ""
            .EvenPage.RightFooter.Text = ""
            .FirstPage.LeftHeader.Text = ""
            .FirstPage.CenterHeader.Text = ""
            .FirstPage.RightHeader.Text = ""
            .FirstPage.LeftFooter.Text = ""
            .FirstPage.CenterFooter.Text = ""
            .FirstPage.RightFooter.Text = ""
        End With
        Application.PrintCommunication = True
    Application.ScreenUpdating = True
    End Function

  17. #17
    Registered User
    Join Date
    04-06-2016
    Location
    pennsylvania
    MS-Off Ver
    excel 2010
    Posts
    9

    Re: creating macro to print all columns but only rows with data

    I think I finally got it working. Thank you for all of your help! I truly appreciate it!

+ 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. [SOLVED] How to print rows with a formula but no data without using VBA or a macro?
    By coss21 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 08-25-2014, 03:02 PM
  2. Need help creating macro to hide rows based upon whether 1 of 3 rows have data
    By BigEdJr in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-17-2014, 02:58 PM
  3. [SOLVED] combine many columns data into half columns data by creating rows
    By killerware in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-08-2013, 07:53 AM
  4. Creating table out of columns of data with macro
    By Jszota in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-22-2011, 10:52 AM
  5. Macro for copying columns, rows, creating sheets, by criteria
    By JCOENG in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-08-2010, 02:04 PM
  6. Replies: 4
    Last Post: 04-22-2010, 10:32 PM
  7. [SOLVED] creating columns with data from offset rows
    By Scott Kelley in forum Excel General
    Replies: 2
    Last Post: 11-13-2005, 10:20 PM

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