Chỉ cho hiện một số worksheet - Show defined worksheet

Liên hệ QC

levanduyet

Hãy để gió cuốn đi.
Thành viên danh dự
Tham gia
30/5/06
Bài viết
1,798
Được thích
4,700
Giới tính
Nam
Vấn đề:

Trong các ứng dụng Excel, trong nhiều tình huống các bạn chỉ cho hiện ra một hay một số worksheet mà thôi.
Vậy đoạn mã đó như thế nào?

Các bạn có thể dùng đoạn mã dưới đây:

Mã:
'[COLOR="Blue"] Thủ tục ViewWs nhằm chỉ cho hiện ra một số worksheet[/COLOR]
' [COLOR="Blue"]theo yêu cầu của mình mà thôi.[/COLOR]

Sub [COLOR="Red"][B]ViewWs[/B][/COLOR](ByVal wsName As Variant)
    Dim ws     As Worksheet

    On Error Resume Next

    Application.ScreenUpdating = False

  [COLOR="Teal"]  'Có ít nhất một worksheet là visible trước
    'Nếu không, lỗi sẽ xãy ra[/COLOR]
    For Each ws In ThisWorkbook.Worksheets
        If IsItInArray(ws.Name, wsName) Then
            ws.Visible = xlSheetVisible
        End If
    Next
    For Each ws In ThisWorkbook.Worksheets
        If Not IsItInArray(ws.Name, wsName) Then
            ws.Visible = xlSheetHidden
        End If
    Next
    
    [COLOR="Teal"]'Đối với các phiên bản Excel cũ
    'Thường sẽ xãy ra lỗi Take Panel
    'Mặc dù đoạn mã trên chẳng dính dáng gì với Task Panel
    'Tôi đã thử tìm kiếm trên internet nhưng ít thấy nói về vấn đề này
    '
    'Tạm thời có thể tham khảo tại đây (cũng chưa hài lòng)
    'Reference: http://support.microsoft.com/kb/288542
    'Toggle the Visible property of the Task Pane to refresh the view.
    'If the Task Pane is visible while the New Item page is active,
    'any changes you make through code are not seen until it is hidden
    'and then made visible again.[/COLOR]
    With Application
        .CommandBars("Task Pane").Visible = True 'Với Excel 2007 không cần dòng này, đã thử
        .CommandBars("Task Pane").Visible = False 'Với Excel 2007 không cần dòng này, đã thử
        .ScreenUpdating = True
    End With
End Sub

'Hàm [COLOR="Red"]IsItInArray [/COLOR]nhằm kiểm tra [COLOR="Blue"]sValueToFind  [/COLOR]có trong mảng [COLOR="Blue"]InputArray [/COLOR]này hay không.
'
Function [B][COLOR="red"]IsItInArray[/COLOR][/B](sValueToFind As Variant, InputArray As Variant) As Boolean 

   IsItInArray = Not IsError(Application.Match(sValueToFind, InputArray, 0)) 

End Function

Ví dụ để sử dụng hàm ở trên:

Giả sử trong workbook tôi đang làm việc có các worksheet sau: "Main", "Data1", "Data2", "Report1", "Report2"
Tôi muốn chỉ hiện ra worksheet "Main""Report1" thì tôi sẽ dùng đoạn mã sau:

Mã:
Sub Test() 
   ViewWs(Array("Main","Report1"))
End Sub

Tham khảo thêm tại đây.

Lê Văn Duyệt
 

File đính kèm

  • ViewWs.xls
    43.5 KB · Đọc: 683
Lần chỉnh sửa cuối:
Web KT
Back
Top Bottom