Error msg in VBA code for clearing data field

masud8956

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

I am using the following code to clear a certain data field. I am applying the code with a button and it worked for a few days but now I am getting an error msg.

VBA 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

What could have gone wrong and how to debug it?

Regards
 
@Fluff, yes workbook is protected with a password. I disabled the protection and the code runs fine now.

Thanks everyone for your advice!

Just curious to know, is there a way to run the code while keeping the protection on?
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Is it protected with a password?
 
Upvote 0
Just curious to know, is there a way to run the code while keeping the protection on?
You would simply include lines of code in your VBA that unprotects the workbook, runs the other code, and then re-protects it afterwards.
 
Upvote 0
In that case you can put this at the start of your code
VBA Code:
ThisWorkbook.Unprotect "Pword"
and this at the end
VBA Code:
ThisWorkbook.Protect "Pword", True, False
Just Pword to whatever your password is.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,825
Members
449,096
Latest member
Erald

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