Consulting

Results 1 to 7 of 7

Thread: VBA macro to set up dynamic print area

  1. #1
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    3
    Location

    VBA macro to set up dynamic print area

    I am trying to create a macro that sets a dynamic print area. There is a defined number of columns but an undefined number of rows. The print area should find the row with the last data point between a range of columns. I've attached a file to show what I mean. The columns that will have data (numbers and text) are C to K. In the file, the last data point is in D46, so the print area should be A1:K46.

    How can I create a macro that sets the print area from A1 to K(?), where (?) is the last row with data in the columns C to K?

    I'm no VBA expert but this would be very helpful. Any thoughts?

    Thanks
    Attached Files Attached Files

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub SetPA()
    Rw = Range("C:K").Find("*", [C1], , xlPart, xlByRows, xlPrevious).Row
    ActiveSheet.PageSetup.PrintArea = "$A$1:$K$" & Rw
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3

  4. #4
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    3
    Location
    The formula works if the cells below don't have any formulas in them. How can the "*" be replaced with something that will only count data as the formulas that yield actual values?

  5. #5
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    3
    Location
    Basically the formulas are copied down all throughout the sheet, but they stop yielding values after a certain point. How do we change "*" so that it only includes data points that came from the formulas?

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Find(What:="=", Lookin:=xlFormulas, ...
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    For the future, the purpose of a sample file is to represent your actual workbook, otherwise solutions may not fit.
    I'm not clear on posts 4 & 5. You want the last cell derived from a formula and to exclude any cells with Constants?

    Assuming that is incorrect
    Sub SetPA()
        Rw = Range("C:K").Find("*", [C1], xlValues, xlPart, xlByRows, xlPrevious).Row
        ActiveSheet.PageSetup.PrintArea = "$A$1:$K$" & Rw
        MsgBox Range("Print_Area").Address
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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