Results 1 to 2 of 2

Need Fix for Code To Print Selected Rows On One Sheet

Threaded View

  1. #1
    Registered User
    Join Date
    11-01-2010
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2007
    Posts
    1

    Need Fix for Code To Print Selected Rows On One Sheet

    I found the following code that prints selected rows on a single sheet. It doesn't work quite like I need it to.

    There are two main issues:

    1. The blank rows between the selected rows are not removed (i.e. if I select row 3 and row 7, there are 3 blanks lines between the printed rows).

    2. It only prints in portrait mode. I need it to print in landscape.

    I am not a VBA programmer and know nothing about it. I am asking if someone can add to or fix the code so it works like I need it to.

    Sub PrintSelectedCells()
    ' prints selected cells, use from a toolbar button or a menu
    Dim aCount As Integer, cCount As Integer, rCount As Integer
    Dim i As Integer, j As Long, aRange As String
    Dim rHeight() As Single, cWidth() As Single
    Dim AWB As Workbook, NWB As Workbook
        If UCase(TypeName(ActiveSheet)) <> "WORKSHEET" Then Exit Sub
        ' useful only in worksheets
         aCount = Selection.Areas.Count
        If aCount = 0 Then Exit Sub ' no cells selected
        cCount = Selection.Areas(1).Cells.Count
        If aCount > 1 Then ' multiple areas selected
            Application.ScreenUpdating = False
            Application.StatusBar = "Printing " & aCount & " selected areas..."
            Set AWB = ActiveWorkbook
            rCount = ActiveSheet.Cells.SpecialCells(xlLastCell).Row
            cCount = ActiveSheet.Cells.SpecialCells(xlLastCell).Column
            ReDim rHeight(rCount)
            ReDim cWidth(cCount)
            For i = 1 To rCount
                ' find the row height of every row in the selection
                rHeight(i) = Rows(i).RowHeight
            Next i
            For i = 1 To cCount
                ' find the column width of every column in the selection
                cWidth(i) = Columns(i).ColumnWidth
            Next i
            Set NWB = Workbooks.Add ' create a new workbook
            For i = 1 To rCount ' set row heights
                Rows(i).RowHeight = rHeight(i)
            Next i
            For i = 1 To cCount ' set column widths
                Columns(i).ColumnWidth = cWidth(i)
            Next i
            For i = 1 To aCount
                AWB.Activate
                aRange = Selection.Areas(i).Address
                ' the range address
                Range(aRange).Copy ' copying the range
                NWB.Activate
                With Range(aRange) ' pastes values and formats
                    .PasteSpecial Paste:=xlValues, Operation:=xlNone, _
                        SkipBlanks:=True, Transpose:=False
                    .PasteSpecial Paste:=xlFormats, Operation:=xlNone, _
                        SkipBlanks:=True, Transpose:=False
                End With
                Application.CutCopyMode = False
            Next i
            NWB.Printout 
            NWB.Close False ' close the temporary workbook without saving
            Application.StatusBar = True
            AWB.Activate
            Set AWB = Nothing
            Set NWB = Nothing
        Else
            If cCount < 90 Then ' less than 90 cells selected
                If MsgBox("Are you sure you want to print " & _
                    cCount & " selected cells ?", _
                    vbQuestion + vbYesNo, "Print celected cells") = vbNo Then Exit Sub
            End If
            Selection.PrintOut
        End If
    End Sub
    Last edited by elance; 11-01-2010 at 09:40 PM. Reason: Required

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