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
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
What is the exact error message you are getting?
Are any of the sheets hidden when you first try running the code?
 
Upvote 0
@Joe4 The msg I am getting is:

"Run-time error '1004':
Unable to set the visible property of the worksheet class"

@afonsomira No I have not.
 
Upvote 0
@Joe4 , sorry did not answer the 2nd Question.

The sheets are hidden and I do not remember exactly if they were hidden during my 1st attempt.
 
Upvote 0
If you hit debug, which line of code does it highlight?

I don't know if this code is sufficient to unhide "very hidden" sheets:
VBA Code:
Sheets("DATA").Visible = True

I think you need to do it as shown here:

 
Last edited:
Upvote 0
If you try using the code found in the link I gave you to unhide all the sheets, you can what their names are:
VBA Code:
Sub UnhideVeryHiddenSheets()
    Dim wks As Worksheet
 
    For Each wks In Worksheets
        If wks.Visible = xlSheetVeryHidden Then wks.Visible = xlSheetVisible
    Next
End Sub
 
Upvote 0
If there was no sheet called DATA I would expect an RTE 9 Subscript out of range.
@masud8956
Check that you have not protected the workbook
 
Upvote 0
Solution

Forum statistics

Threads
1,215,467
Messages
6,124,984
Members
449,201
Latest member
Lunzwe73

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