Chủ Nhật, 25 tháng 3, 2012

In nhiều vùng được lựa chọn trong một Sheet


Nếu như chọn nhiều vùng dữ liệu trong một Sheet và khi ra lệnh in đánh dấu in phần Selection thì chỉ cho phép bạn in chỉ được một vùng dữ liệu đã được lựa chọn mà thôi .




Vậy làm thế nào để có thể in được nhiều vùng dữ liệu được lựa chọn trong một Sheet  ? Bạn phải dùng Macro , cách thức như sau
1. Mở file Excel làm việc .
2. Bấm tổ hợp phím Alt_F11 để mở cửa sổ Microsoft Visual Basic
3. Bấm menu Insert , chọn Module , bạn cắt dán theo nội dung dưới đây

Sub PrintSelectedCells()
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
        aCount = Selection.Areas.Count
    If aCount = 0 Then Exit Sub
    cCount = Selection.Areas(1).Cells.Count
    If aCount > 1 Then
        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   
            rHeight(i) = Rows(i).RowHeight
        Next i
        For i = 1 To cCount
           
            cWidth(i) = Columns(i).ColumnWidth
        Next i
        Set NWB = Workbooks.Add
        For i = 1 To rCount
            Rows(i).RowHeight = rHeight(i)
        Next i
        For i = 1 To cCount
            Columns(i).ColumnWidth = cWidth(i)
        Next i
        For i = 1 To aCount
            AWB.Activate
            aRange = Selection.Areas(i).Address          
            Range(aRange).Copy
            NWB.Activate
            With Range(aRange)
                .PasteSpecial Paste:=xlValues, Operation:=xlNone, _
                    SkipBlanks:=False, Transpose:=False
                .PasteSpecial Paste:=xlFormats, Operation:=xlNone, _
                    SkipBlanks:=False, Transpose:=False
            End With
            Application.CutCopyMode = False
        Next i
        NWB.PrintOut
        NWB.Close False
        Application.StatusBar = False
        AWB.Activate
        Set AWB = Nothing
        Set NWB = Nothing
    Else
        If cCount < 10 Then
            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

4. Quay trở về cửa sổ Excel
5. Chọn những vùng dữ liệu bạn định in . Đánh dấu nó dựa trên kết hợp giữ phím Ctrl và chuột
6. Bấm menu Tools > Macro > Macros và chạy Macro có tên gọi PrintSelectedCells

Không có nhận xét nào:

Đăng nhận xét