Link to home
Start Free TrialLog in
Avatar of srikanthv2322
srikanthv2322

asked on

Print specified range of cells content in a spreadsheet

I want to give a print out to local printer that will print the given range of cells content with the same style guide/Format in the spreadsheet.

EX: If i have B5:G20 is the range of cell content with given format/style property to be printed at a click of command button through local printer
Avatar of mscanlon06851
mscanlon06851

See if this does the trick for you.

I'm assuming you have a specific page in a specific workbook to which you want to add the command button and that you will be reusing this page for this purpose repeatedly.

First, embed a control button in the upper left hand corner of the page

-right-click on a blank section in the toolbar area at the top of the page (to display a list of all toolbars)
-click on "Control Toolbox"  if it's not already checked (to display the list of available toolbars)
-(drag the toolbar to top of the screen to "dock" it, if you want)
-click on the "command button" control on the Control Toolbox toolbar
-drag the button to wherever you want to place it permanently
-right-click on the command button
-click Properties
-change the Name value to "cmdPrintSelection"
-change the Caption value to "Print Selection"
-close the Properties window
-expand the size of the button, if text doesn't fit

Next, add code to the button to generate the printout

-double-click on the Print Selection command button (to open a code window in the VB Editor)
-immedidately below the line that says "Private Sub cmdPrintSelection_Click()", paste in the following code
   
    Dim Response As Integer
   
    Response = _
        MsgBox("Print cells " & Chr(10) & Chr(10) & Selection.Address & " ?", _
        vbOKCancel, "Print Cells")
    If Response = vbCancel Then
        Exit Sub
    End If
   
    ActiveSheet.PageSetup.PrintArea = Selection.Address
    ActiveWindow.SelectedSheets.PrintOut
    ActiveSheet.PageSetup.PrintArea = ""

-be sure the words "End Sub" follow the above code
-return to the workbook
-click on the first icon on the Control Toolbox toolbar (to "Exit Design Mode")
-save the workbook

To print a range of cells:

-using the mouse, select the range of cells to be printed
-click the "Print Selection" button

Note that the report will be sent directly to the default printer, so be sure the workbook is pointing to the correct printer prior to the first time you click on the command button.

Or, you can just use the attachment
 PrintSelection.xls
Avatar of Ingeborg Hawighorst (Microsoft MVP / EE MVE)
Why do you need a macro do do that?

Just define the cells you want to print as the Print Area and whenever you want to print them, click the Print button that's already a part of the user interface.

What am I not getting?

cheers, teylyn
Your point is well taken, teylyn.

My verbose answer was in response to the stated request for a command button that would  perform the operation with a single click.   If I mis-interpreted the intent of the request, then I cheerfully concur with -- and recommend -- your solution.

Regards,

ms


Well, there could be more to it than I see. I have this little private mission to discourage re-inventing the wheel with macros when Excel has the functionality and  buttons already in place.

If a Print Area is already set to a different range, and the objective is to use a button to print a different range, disregarding the Print Area, I guess the request -- and the solution suggested -- makes perfect sense.

cheers, teylyn
ASKER CERTIFIED SOLUTION
Avatar of mscanlon06851
mscanlon06851

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial