提示:其他語言是Google翻譯的。 你可以訪問 English 版本。
登入
x
or
x
x
註冊
x

or

如何在受保護的工作表中對行進行分組和取消分組?

眾所周知,在受保護的工作表中,我們應用某些操作有很多限制。 例如,我們無法在分組和未分組的數據之間切換。 有沒有辦法在受保護的工作表中對行進行分組或取消分組?

使用VBA代碼對受保護的工作表中的行進行分組和取消分組

Office選項卡在Office中啟用選項卡式編輯和瀏覽,使您的工作更輕鬆......
Kutools for Excel - 最佳辦公生產力工具將解決您的大部分Excel問題
  • 重用任何東西: 將最常用或最複雜的公式,圖表和其他任何內容添加到您的收藏夾中,並在將來快速重複使用它們。
  • 超過20文本功能: 從文本字符串中提取數字; 提取或刪除部分文本; 將數字和貨幣轉換為英語單詞...
  • 合併工具:多個工作簿和表格合二為一; 合併多個單元格/行/列而不丟失數據; 合併重複行和總和...
  • 拆分工具:根據價值將數據拆分為多個表格; 一個工作簿到多個Excel,PDF或CSV文件; 一列到多列......
  • 粘貼跳過 隱藏/過濾行; 數和總和 按背景顏色; 創建郵件列表和 通過Cell的價值發送電子郵件...
  • 超級過濾器: 創建高級過濾方案並應用於任何工作表; 分類 按週,日,頻率等; 過濾 通過大膽,公式,評論......
  • 超過300強大的功能; 適用於Office 2007-2019和365; 支持所有語言; 在公司輕鬆部署; 全功能60天免費試用。

箭頭藍色右泡 使用VBA代碼對受保護的工作表中的行進行分組和取消分組


也許,沒有其他好方法可以解決這個問題,但使用VBA代碼,請按照以下步驟操作:

1。 激活您想要使用的工作表,請確保工作表尚未受到保護。

2。 然後按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications窗口.

3。 點擊 插入 > 模塊,並將以下代碼粘貼到 模塊窗口.

VBA代碼:在受保護的工作表中分組和取消分組

Sub EnableOutlining()
'Update 20140603
Dim xWs As Worksheet
Set xWs = Application.ActiveSheet
Dim xPws As String
xPws = Application.InputBox("Password:", xTitleId, "", Type:=2)
xWs.Protect Password:=xPws, Userinterfaceonly:=True
xWs.EnableOutlining = True
End Sub

4. 然後按 F5 鍵運行此代碼,並彈出提示框提醒您輸入密碼以保護當前工作表。 看截圖:

DOC組,在保護的sheet1

5。 然後點擊 OK,您的工作表已被保護,但您可以在此受保護的工作表中展開和縮小輪廓符號,請參閱截圖:

DOC組,在保護的sheet1

注意:如果您的工作表已被保護,則此代碼將無法使用。


Kutools for Excel - 最佳辦公生產力工具提高80%的生產力

  • 重用: 快速插入 複雜的公式,圖表 以及你以前用過的任何東西; 加密單元格 密碼; 創建郵件列表 並發送電子郵件...
  • 超級方程式酒吧 (輕鬆編輯多行文字和公式); 閱讀佈局 (輕鬆讀取和編輯大量單元格); 粘貼到過濾範圍...
  • 合併單元格/行/列 不丟失數據; 分裂細胞含量; 組合重複的行/列...防止重複的細胞; 比較範圍...
  • 選擇複製或唯一 行; 選擇空行 (所有細胞都是空的); 超級查找和模糊查找 在許多工作簿中; 隨機選擇......
  • 精確複製 多個單元格而不更改公式參考; 自動創建參考 多張表; 插入項目符號,複選框等等......
  • 提取文本,添加文本,按位置刪除, 刪除空間; 創建和打印分頁小計; 在單元格內容和註釋之間轉換...
  • 超級過濾器 (將過濾方案保存並應用到其他工作表); 高級排序 按月/週/日,頻率等; 特殊過濾器 用粗體,斜體......
  • 結合工作簿和工作表; 根據鍵列合併表; 將數據拆分為多個表格; 批量轉換xls,xlsx和PDF...
  • 超過300強大的功能。 支持Office / Excel 2007-2019和365。 支持所有語言。 在您的企業或組織中輕鬆部署。 全功能60天免費試用。
kte tab 201905

Office選項卡為Office提供選項卡式界面,使您的工作更輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,Publisher,Access,Visio和Project。
  • 在同一窗口的新選項卡中打開並創建多個文檔,而不是在新窗口中。
  • 通過50%提高您的工作效率,每天為您減少數百次鼠標點擊!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Dean Fabella · 5 months ago
    How To Group And Ungroup Rows and Columns In Protected Worksheet?
  • To post as a guest, your comment is unpublished.
    Jorge · 7 months ago
    formidable
  • To post as a guest, your comment is unpublished.
    EddieYeah · 8 months ago
    Someone might need this, I think I figured out how to make this work.

    First, your code needs to be written in "ThisWorkbook" under Microsoft Excel Objects, as @peachyclean suggests.
    Second, take the code that @Sravanthi wrote, and paste to the above mentioned location.

    Sub Workbook_Open()
    'Update 20140603
    Dim xWs As Worksheet
    Set xWs = Application.ActiveSheet
    Dim xPws As String
    xPws = "rfc" ''Application.InputBox("Password:", xTitleId, "", Type:=2)
    xWs.Protect Password:=xPws, Userinterfaceonly:=True
    xWs.EnableOutlining = True
    End Sub

    The thing is that you need to be on the sheet which you want to protect but allowing using grouping, and save the workbook and close, without protecting. Now if you open it, the macro starts automatically, it will make the sheet protected with the password "rfc". Now you can use the grouping, the sheet is protected.

    For my solution, I've modified the password applied, so you can rewrite any password HERE:
    xPws = "WRITEANYPASSWORDHERE" ''Application.InputBox("Password:", xTitleId, "", Type:=2)

    Furthermore, I didn't want the to-be-protected sheet active when opening the file, therefore I've modified this part:
    Set xWs = Application.ActiveSheet ->
    Set xWs = Application.Worksheets("WRITEANYSHEET'SNAMEHERE")

    Now it works like charm, sheet named 'WRITEANYSHEET'SNAMEHERE' is protected but the grouping applicable. On the long run, I think the problem will be that if I want to modify this file and keep the solution, I need to unprotect this sheet to make it work on the next opening. I guess you can write another macro to automatically unprotect when closing :)


    I hope it helped.
  • To post as a guest, your comment is unpublished.
    Denise · 1 years ago
    do you have visuals for the VBA Code discussed 6 days ago to peachyclean about ThisWorkbook under Microsoft Objects instead of a new module. The functionality is lost when I go back into my workbook
  • To post as a guest, your comment is unpublished.
    peachyclean · 1 years ago
    To fix the issue of this not working in your file after you've closed it and opened it again, you have to paste the VBA code in "ThisWorkbook" under Microsoft Excel Objects instead of a new module. This will then automatically run the macro every time the file is opened.
  • To post as a guest, your comment is unpublished.
    Susan · 1 years ago
    I have gotten this code to work. But when I close and reopen I must go to the developer tab, select the macros button, select run and enter the password.

    Is there a way to remove the password from the code OR a auto run code that will automatically run the this marco and enter the password?
  • To post as a guest, your comment is unpublished.
    Sravanthi · 1 years ago
    Sub Workbook_Open()
    'Update 20140603
    Dim xWs As Worksheet
    Set xWs = Application.ActiveSheet
    Dim xPws As String
    xPws = "rfc" ''Application.InputBox("Password:", xTitleId, "", Type:=2)
    xWs.Protect Password:=xPws, Userinterfaceonly:=True
    xWs.EnableOutlining = True
    End Sub
  • To post as a guest, your comment is unpublished.
    Neelash · 1 years ago
    this works for a bit, once you close and reopen, it stops :(
  • To post as a guest, your comment is unpublished.
    Arav · 2 years ago
    When i use the command, i see a error message as shown below:
    Private Sub Workbook_Open()
    Dim wsh As Variant
    For Each wsh In Worksheets(Array("TD_ phase_3", "RS_Phase_2"))
    wsh.EnableOutlining = True
    wsh.Protect Password:="260615", DrawingObjects:=False, _
    contents:=True, _
    Scenarios:=True, _
    AllowFiltering:=True, _
    AllowFormattingCells:=True, _
    userinterfaceonly:=True
    Next wsh
    End Sub
    Run time error '9':
    Subscript out of range
  • To post as a guest, your comment is unpublished.
    Tom · 2 years ago
    How can I change the password to another value?
  • To post as a guest, your comment is unpublished.
    Tom · 2 years ago
    Has it been asked / answered? Where in the code can you specify / change a personal password?
  • To post as a guest, your comment is unpublished.
    Emmanuel Nyemah · 3 years ago
    Hello, please help me expand and collapse rolls and collumns in excel spreadsheet that is protected. I tried using the ones you showed above but they do not work.
  • To post as a guest, your comment is unpublished.
    Emmanuel Nyemah · 3 years ago
    Please help me, I want to collapse and expand some rolls and collumns in an excel spreadsheet that is protected. How can I use macros to do this? I have tried what you showed but they just don't work on my spreadsheet. Please help.
  • To post as a guest, your comment is unpublished.
    Steph · 4 years ago
    Hello! I used the first macro with success and then had the same issue of closing the workbook and the macro no longer working. I see the solution above but cannot get that to work at all. Would you mind stepping me through? Do I combine both codes or just use the latter? If my password is "dog" do I replace one of the values in the code? I am only applying to one worksheet ("Sheet1"); do I use that anywhere? Many thanks in advance!!
  • To post as a guest, your comment is unpublished.
    Chiu · 4 years ago
    Private Sub Workbook_Open()
    Dim wsh As Variant
    For Each wsh In Worksheets(Array("TD_ phase_3", "RS_Phase_2"))
    wsh.EnableOutlining = True
    wsh.Protect Password:="260615", DrawingObjects:=False, _
    contents:=True, _
    Scenarios:=True, _
    AllowFiltering:=True, _
    AllowFormattingCells:=True, _
    userinterfaceonly:=True
    Next wsh
    End Sub
    • To post as a guest, your comment is unpublished.
      Josh · 2 years ago
      Can you do a step by step walk thru as to where to put this as they did in the original instructions. Thank you.
    • To post as a guest, your comment is unpublished.
      jgarner · 2 years ago
      Still not sure how this works. Do i make a new module or attach to the one above?
  • To post as a guest, your comment is unpublished.
    Phi Bach · 4 years ago
    I have the same problem when i close the workbook. Any ideas to fix it?
  • To post as a guest, your comment is unpublished.
    Nauman · 5 years ago
    Thank you so much bro this works really nice.Thanks alot
    • To post as a guest, your comment is unpublished.
      jgarner · 2 years ago
      How did you get this to work? I've tried adding it to the VBA above and making a different module but it still doesnt work. Do i need to change any of the codes? like my password i am using or do i need to change the sheet names?
  • To post as a guest, your comment is unpublished.
    mayich · 5 years ago
    This seems to work great, but when I close and re-open the workbook, I run into the same problem - I cannot expand my collapsed groups.
    • To post as a guest, your comment is unpublished.
      Chiu · 4 years ago
      [quote name="mayich"]This seems to work great, but when I close and re-open the workbook, I run into the same problem - I cannot expand my collapsed groups.[/quote]That matter is solved as bellow
      Private Sub Workbook_Open()
      Dim wsh As Variant
      For Each wsh In Worksheets(Array("Sheet1", "Sheet2"))
      wsh.EnableOutlining = True
      wsh.Protect Password:="260615", DrawingObjects:=False, _
      contents:=True, _
      Scenarios:=True, _
      AllowFiltering:=True, _
      AllowFormattingCells:=True, _
      userinterfaceonly:=True
      Next wsh
      End Sub
      • To post as a guest, your comment is unpublished.
        Morne · 2 years ago
        I got the same problem, as soon as I close and re-enter sheet, it doesn't work... please give step by step of where and how to use
        That matter is solved as bellow
        Private Sub Workbook_Open()
        Dim wsh As Variant
        For Each wsh In Worksheets(Arra y("Sheet1", "Sheet2"))
        wsh.EnableOutli ning = True
        wsh.Protect Password:="2606 15", DrawingObjects: =False, _
        contents:=True, _
        Scenarios:=True, _
        AllowFiltering:=True, _
        AllowFormattingCells:=True, _
        userinterfaceonly:=True
        Next wsh
        End Sub
    • To post as a guest, your comment is unpublished.
      Romi · 5 years ago
      I have the same problem, does anyone know how to overcome it.

      Many thanks