Hide and Protect workbook

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello code experts,
I have this code which runs correctly. But when I run the code after protecting the workbook it is showing the wrong results. I need your expertise to help me to edit the code.
In the Data sheet, I want to keep columns B2:G51 unhidden and unprotected. The rest of the rows and columns I want to hide. I also want to hide the Extract and Import sheets. The user has to just enter the data in columns B2: G51 only and get the xml generated. I also want the code to be protected in the VBA project Properties.
Password 123
Hide and protect sheet.xlsm
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Rich (BB code):
Option Explicit

Sub HideSheets()
'shared by Alana
    Dim ws As Worksheet
    ThisWorkbook.Unprotect Password:=123
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Data" Then
            ws.Protect Password:=123
            ws.Visible = xlSheetVeryHidden
        End If
    Next ws
    ThisWorkbook.Protect Password:=123
End Sub

Sub UnHideSheets()
    Dim ws As Worksheet
    ThisWorkbook.Unprotect Password:=123
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Data" Then
            ws.Visible = xlSheetVisible
            ws.Unprotect Password:=123
        End If
    Next ws
    'ThisWorkbook.Protect Password:=123
End Sub
This code is unprotecting the workbook but I have some cells in the data sheet Locked and Hidden in the Data sheet. It is not unlocking the hidden cells and getting the data from the data sheet to the Extract sheet.
In short, I am able to get data till 51 rows whereas I have to get data from 365 rows.
How do I edit the code to unprotect in the beginning and protect again in the end.?
 
Upvote 0
The above issue is solved now.

FYI, you can mark the thread as "Solved" by clicking the the "Mark as solution" (checkmark) button to the right of the post which answers the question.

1664721171626.png
 
Upvote 0
There is no post that contains a solution, so there is nothing to mark.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top