+ Reply to Thread
Results 1 to 13 of 13

Macro to print selected area

  1. #1
    Registered User
    Join Date
    06-12-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    10

    Macro to print selected area

    Hi, I am trying to create a macro where after selecting a named range, it would print the selected range. I have a worksheet divided in different ranges formated for printing. Each group of cell is named in order to make selection easier. I would like to create a macro where either excel prompts the user to select the named ranges, or cell ranges, and then print, or, after selecting a named range, it prints. I use Excel 2010. I tried to create it using the macro recorder, but it doesn't work. Any sugestions?

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to print selected area

    This should print the selected range.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-12-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Macro to print selected area

    Thanks AlphaFrog, I finally did that, because I erased part of the code, after running the macro recorder. But it still wonder if I can make excel prompt to ask for a range. I mean, get a dialog box (i think it is an input box) with a message "Enter range name or cells", the user enter the range name or cells and it prints the selected range.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to print selected area

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 06-13-2012 at 10:48 AM.

  5. #5
    Registered User
    Join Date
    06-12-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Macro to print selected area

    Thanks AlphaFrog, it worked perfect! Now, I have a final question, somehow related. I want to do a macro, where excel promts to enter a range, but for a function. Lets say, for SUM, or AVG, or COUNT,

    Thank you very much for all the help

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to print selected area

    You're welcome.

    Your new request is too vague. I don't know how to code "Lets say". You need to be very specific; Prompt to select the range, Sum the selection, then do what with the sum?

    The code would be nearly identical to the Print code above except instead of print, you do something else. Let's say Sum.

  7. #7
    Registered User
    Join Date
    06-12-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Macro to print selected area

    Sorry for the vagueness, I'm very new in this programming thing. I tried this:

    "Sub Prompt_to_Select_SUM_Range()


    Dim rng As Range, strRng As String

    Do

    strRng = Application.InputBox("Type in a Named Range or a cell range. ", "Select a Range to SUM", "A1:B20", Type:=2)

    If strRng = "False" Then Exit Sub 'User canceled

    On Error Resume Next
    Set rng = Range(strRng)
    On Error GoTo 0

    If rng Is Nothing Then MsgBox "Cannot define '" & strRng & "' as a range. ", , "Invalid Range"

    Loop Until Not rng Is Nothing

    'rng.PrintPreview
    'rng.PrintOut Copies:=1, Collate:=True
    'ActiveSheet.DisplayAutomaticPageBreaks = False
    rng.Calculate
    End Sub"

    But it didn't work, I suppose that one of my problems is the rng.Calculate statement, that it may not make sense to VBA, but what I want to do with the sum (average or any other simple function) is to display the result.

    Thank you, I hope this is more specific.

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to print selected area

    Replace...
    rng.Calculate

    With this...
    MsgBox Application.WorksheetFunction.Sum(rng), , "SUM"

  9. #9
    Registered User
    Join Date
    06-12-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Macro to print selected area

    I replaced the rng.Calculate with the statement you posted, but it gives me an error that says "Cannot define '500' (first number on the first cell of the range I'm writing) as a range.

  10. #10
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to print selected area

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    06-12-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Macro to print selected area

    Thank you, it worked. No more questions

  12. #12
    Registered User
    Join Date
    12-20-2013
    Location
    america, america
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: Macro to print selected area

    how do i get the selected range to now fit to one page?

    is there a way to select multiple ranges to print?

    Quote Originally Posted by AlphaFrog View Post
    Please Login or Register  to view this content.

  13. #13
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to print selected area

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

+ 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