Running macros on protected sheets

masud8956

Board Regular
Joined
Oct 22, 2016
Messages
163
Office Version
  1. 2016
  2. 2011
  3. 2007
Platform
  1. Windows
Hi all!

In my workbook I have 5 sheets.

In 2 of the sheets named "DATA" and "DATA COPY" I have imported a large array of data from another folder through macro and used those data in other 3 sheets.

I kept these 2 sheets in "xlSheetVeryHidden" format to deny access to users. In "DATA" and "DATA COPY" sheet I was trying to run a macro to clear contents of the range A1:O4000 (or clear contents of the whole sheets - even better). I want the code to access these 2 sheets, clear contents of the said range (or of the whole sheets) and return to original formatting (xlSheetVeryHidden). I was trying the following code:
Code:
[/COLOR]Sub ClearMe()Application.ScreenUpdating = False
Sheets("DATA").Visible = True
Sheets("DATA COPY").Visible = True
 '
' ClearMe Macro
'


'
    Sheets(Array("DATA", "DATA COPY")).Select
    Sheets("DATA").Activate
    Cells.Select
    Selection.ClearContents
Sheets("DATA").Visible = False
Sheets("DATA COPY").Visible = False
Application.ScreenUpdating = True

End Sub[COLOR=#333333]

The code seems to take about 25-35 seconds to complete the task. But after the run is complete the sheets do not go back to the
original formatting (xlSheetVeryHidden). They become "xlSheetHidden" and thus they can be easily unhidden by users which I do not want.

In fact I bumped into a very old thread (from 2002) regarding this where I took the idea of the code. Link below:
HTML:
https://www.mrexcel.com/forum/excel-questions/31275-running-macros-hidden-sheets.html

What modification could be done to solve the problem? Anyone please?

Thanks in advance
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Change these two lines:
Code:
Sheets("DATA").Visible = False
Sheets("DATA COPY").Visible = False
Replace "False" with "xlVeryHidden" (w/o the quote marks).
 
Upvote 0
@JoeMo,

Thanks a lot! It works perfectly!:)

Just another query, the code takes 25-30 second to complete the task. Is it normal or my code has rooms for improvement?
 
Upvote 0
@JoeMo,

Thanks a lot! It works perfectly!:)

Just another query, the code takes 25-30 second to complete the task. Is it normal or my code has rooms for improvement?
You are welcome.

As to your new question, if you have formulas on the sheets you are clearing try entering:
Code:
Application.Calculation = xlCalculationManual
at the start of the code, and
Code:
Application.Calculation = xlCalculationAutomatic
just before the End Sub line.

If there are no formulas, I would try clearing the two sheets one at a time like this:
Code:
With Sheets("DATA")
      .Cells.ClearContents
End With
With Sheets("DATA COPY")
      .Cells.ClearContents
End With
 
Upvote 0
There are no formulas.

If I have understood correctly, you are advising the following code:
Code:
Sub ClearMe()Application.ScreenUpdating = False
Sheets("DATA").Visible = True
Sheets("DATA COPY").Visible = True
With Sheets("DATA")
      .Cells.ClearContents
End With
With Sheets("DATA COPY")
      .Cells.ClearContents
End With
Sheets("DATA").Visible = xlVeryHidden
Sheets("DATA COPY").Visible = xlVeryHidden
Application.ScreenUpdating = True
End Sub

Is it right?
 
Upvote 0
There are no formulas.

If I have understood correctly, you are advising the following code:
Code:
Sub ClearMe()Application.ScreenUpdating = False
Sheets("DATA").Visible = True
Sheets("DATA COPY").Visible = True
With Sheets("DATA")
      .Cells.ClearContents
End With
With Sheets("DATA COPY")
      .Cells.ClearContents
End With
Sheets("DATA").Visible = xlVeryHidden
Sheets("DATA COPY").Visible = xlVeryHidden
Application.ScreenUpdating = True
End Sub

Is it right?
That's what I'm advising. We won't know if its right until you try it and post back to tell us if it reduces the run time. :)
 
Upvote 0
I tried it. The runtime does not reduce noticeably. Could be my machine's limitation. I am not sure.

The code looks tidy though. Thank you again for your help!:)
 
Upvote 0

Forum statistics

Threads
1,215,426
Messages
6,124,828
Members
449,190
Latest member
rscraig11

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