+ Reply to Thread
Results 1 to 12 of 12

Macro to print variable number of rows

  1. #1
    Registered User
    Join Date
    03-22-2010
    Location
    London
    MS-Off Ver
    2007
    Posts
    49

    Question Macro to print variable number of rows

    Hello everyone,

    I have a worksheet in which rows 1-7 are being used as the 'header', and rows 8-1008 as data.

    Rows 8 and 9 are column headers.

    Row 10 provides a description and instructions on how to interpret the column headers in rows 8-9. (I have hidden this row and have instead chosen to attach the instructions as comments to the specific cells. But it may be the case that I start displaying this again.)

    The data runs from columns A-L.

    I need to print the 'header' rows of 1-7, and then subsequently all the following rows which have been filled in - which might only be row 8, or it might be all the rows up to 1008...

    Additionally, although I don't know if this adds very much to the coding, this will be printed on A3 in landscape.

    I would really appreciate some help...

    My really very basic first attempt is as follows

    Please Login or Register  to view this content.
    And I 'recorded' this macro by selecting cells A2-L2, and then pressing Ctrl+Shift+DownArrow until I got to the last row. However, this currently doesn't work.

    Many thanks in advance

    DP
    Last edited by dpcp; 08-14-2011 at 01:19 PM.

  2. #2
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Macro to print variable number of rows

    Hi,

    You can use the following code to select/define the area to be printed

    Please Login or Register  to view this content.
    Then you just apply it to the printing part of your code manuelly or record it.

    Steffen Thomsen

  3. #3
    Registered User
    Join Date
    03-22-2010
    Location
    London
    MS-Off Ver
    2007
    Posts
    49

    Re: Macro to print variable number of rows

    Quote Originally Posted by Steffen Thomsen View Post
    Hi,

    You can use the following code to select/define the area to be printed

    Please Login or Register  to view this content.
    Then you just apply it to the printing part of your code manuelly or record it.

    Steffen Thomsen
    Hi Steffen

    Thanks so much for your help.

    I have put in the code as you suggested, as follows:

    Please Login or Register  to view this content.
    The issue I am now having is that when I run the macro, it goes to the 'print preview' page, and the only cells that the print preview shows me are
    - the cell which is currently selected (i.e. where my cursor last clicked)
    - the two cells relating to the column headers (rows 8 and 9)

    So if I have C13 selected, print preview shows C8, C9, C13 as the part of the worksheet that will be printed.

    Ideally, I would want to incorporate the macro as a button on the spreadsheet, so that a user can just click 'Print Worksheet' and (instead of coming up with the print preview screen) the worksheet would be sent to the printer...

    Many thanks again for your improvement to my code!

    DP

  4. #4
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Macro to print variable number of rows

    Try using this instead of the print line

    Please Login or Register  to view this content.
    Steffen Thomsen

  5. #5
    Registered User
    Join Date
    03-22-2010
    Location
    London
    MS-Off Ver
    2007
    Posts
    49

    Re: Macro to print variable number of rows

    I've tried it with that code but unfortunately nothing goes to the printer. I also tried closing excel, but still nothing goes to the printer. And as a final (random?) idea, I put

    Please Login or Register  to view this content.
    But that only opened up the debugger...

  6. #6
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Macro to print variable number of rows

    Hi again,

    I made a little mistake, this should work.

    Please Login or Register  to view this content.
    Make sure you have set your printer to default.

    Steffen Thomsen

  7. #7
    Registered User
    Join Date
    03-22-2010
    Location
    London
    MS-Off Ver
    2007
    Posts
    49

    Re: Macro to print variable number of rows

    That did work... but here's the catch -

    Rows 1-16 have text in them, but rows 17-1008 don't. However, there are a set of columns in rows 11-1008 that have been formatted with data validation, so that when users are entering data into the spreadsheet, it is formatted for their use (e.g. with dropdowns).

    So, currently using the above code prints the whole worksheet, rows 1-1008, because most of the rows (even if they are blank) have the data validation.

    Is there a way to select only the rows which have [one or more] cells filled in? so, rows where there is one [or a set of] values in columns A-L.

    Apologies for making this ever more convoluted...!

  8. #8
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Macro to print variable number of rows

    Hi again,

    Do you have a single row that only filled to the size og the printarea?

    Or maby you can upload a dummyworkbook?

    Steffen Thomsen

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Macro to print variable number of rows

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  10. #10
    Registered User
    Join Date
    03-22-2010
    Location
    London
    MS-Off Ver
    2007
    Posts
    49

    Re: Macro to print variable number of rows

    Hello,

    I've attached a dummy worksheet now which has the basic layout.

    Thanks very much!

    DP
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    03-22-2010
    Location
    London
    MS-Off Ver
    2007
    Posts
    49

    Question Re: Macro to print variable number of rows

    Hi,

    I've been working on some further code, and I came up with this:

    Please Login or Register  to view this content.
    But there I get a 'compile' error on the line beginning 'LastRow'...

    I think perhaps it would be a better idea, instead of automatically sending all the rows to 'print', the macro instead goes to the Print Preview screen, so users can see what they will be printing...

    I'd greatly appreciate a bit of help in tweaking with this code because all I seem to be doing with this is just copy+pasting from various other (related and unrelated!) codes I find online to amalgamate into what I need a macro to do...

    Many thanks

    DP

  12. #12
    Registered User
    Join Date
    03-22-2010
    Location
    London
    MS-Off Ver
    2007
    Posts
    49

    Re: Macro to print variable number of rows

    Just to say, I have adapted some code now for the above problem

    Please Login or Register  to view this content.
    This bring up the print preview screen with rows of data that have info in them.

    Thanks a lot for your help

    DP

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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