Run-time error 1004

pbnadia

New Member
Joined
Nov 24, 2014
Messages
5
Hello,

I've created a calendar with macros, however when I try to protect the sheet I get a runtime error. I'm learning macros and have tried various codes but have not helped. Here is the macro I have:
Code:
Sub HideMonths()
Application.ScreenUpdating = False
Dim rngDates As Range, rngMonths As Range, MyCell As Range
Dim strMonth As String, strYear As String, strMonthYear As String
Set rngDates = [Dates].Cells ' individual dates
Set rngMonths = [Months].Cells ' drop-down list
Set rngYears = [Years].Cells ' drop down list
strMonth = WorksheetFunction.Index(rngMonths, Range("C1"))
strYear = WorksheetFunction.Index(rngYears, Range("C2"))
strMonthYear = Format(DateValue(strMonth & " " & strYear), "mmm yyyy")
For Each MyCell In rngDates
    If Format(MyCell, "mmm yyyy") < strMonthYear _
        Or Format(MyCell, "mmm yyyy") > strMonthYear Then
        Columns(MyCell.Column).Hidden = True
    Else
        Columns(MyCell.Column).Hidden = False
        
    End If
Next MyCell
Application.ScreenUpdating = True
End Sub
Here is the error I when I protect the sheet:

Run -time error '1004':
"Unable to set the Hidden property of the Range class"

Hopefully someone can help!

Thanks in advance!
 
Last edited by a moderator:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You have to UNprotect the sheet first
Then hide/unhide columns
Then ReProtect the sheet..

Something like
Code:
Sheets("SheetName").UnProtect "PasswordGoesHere"
For Each MyCell In rngDates
    If Format(MyCell, "mmm yyyy") < strMonthYear _
        Or Format(MyCell, "mmm yyyy") > strMonthYear Then
        Columns(MyCell.Column).Hidden = True
    Else
        Columns(MyCell.Column).Hidden = False

    End If
Next MyCell
Sheets("SheetName").Protect "PasswordGoesHere"
 
Upvote 0
Hello Jonmo1,

Thank you for your quick response. I added the code above my code a and get "Compile error: Invalid outside procedure" where the "password" goes. I do enter my password there correct?
this is what I have:

Sheets("2015").Unprotect "passwordgoeshere"
For Each MyCell In rngDates
If Format(MyCell, "mmm yyyy") < strMonthYear _
Or Format(MyCell, "mmm yyyy") > strMonthYear Then
Columns(MyCell.Column).Hidden = True
Else
Columns(MyCell.Column).Hidden = False
End If
Next MyCell
Sheets("2015").Protect "Passwordgoeshere"
Sub HideMonths()
Application.ScreenUpdating = False
Dim rngDates As Range, rngMonths As Range, MyCell As Range
Dim strMonth As String, strYear As String, strMonthYear As String
Set rngDates = [Dates].Cells ' individual dates
Set rngMonths = [Months].Cells ' drop-down list
Set rngYears = [Years].Cells ' drop down list
strMonth = WorksheetFunction.Index(rngMonths, Range("C1"))
strYear = WorksheetFunction.Index(rngYears, Range("C2"))
strMonthYear = Format(DateValue(strMonth & " " & strYear), "mmm yyyy")
For Each MyCell In rngDates
If Format(MyCell, "mmm yyyy") < strMonthYear _
Or Format(MyCell, "mmm yyyy") > strMonthYear Then
Columns(MyCell.Column).Hidden = True
Else
Columns(MyCell.Column).Hidden = False

End If
Next MyCell
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Looks like you copied my whole code and pasted it into your module above the beginning of your original code.

You need to put the UnProtect and Protect lines INSIDE your orginal code.
 
Upvote 0
Try
Rich (BB code):
Sub HideMonths()
 Application.ScreenUpdating = False
 Dim rngDates As Range, rngMonths As Range, MyCell As Range
 Dim strMonth As String, strYear As String, strMonthYear As String
 Set rngDates = [Dates].Cells ' individual dates
 Set rngMonths = [Months].Cells ' drop-down list
 Set rngYears = [Years].Cells ' drop down list
 strMonth = WorksheetFunction.Index(rngMonths, Range("C1"))
 strYear = WorksheetFunction.Index(rngYears, Range("C2"))
 strMonthYear = Format(DateValue(strMonth & " " & strYear), "mmm yyyy")
Sheets("SheetName").UnProtect "PasswordGoesHere"
 For Each MyCell In rngDates
 If Format(MyCell, "mmm yyyy") < strMonthYear _
 Or Format(MyCell, "mmm yyyy") > strMonthYear Then
 Columns(MyCell.Column).Hidden = True
 Else
 Columns(MyCell.Column).Hidden = False

 End If
 Next MyCell
Sheets("SheetName").Protect "PasswordGoesHere"
 Application.ScreenUpdating = True
 End Sub

make sure you actually put in the valid password where I highlighted in red.
 
Upvote 0
Thank you again for helping me last time.. I have another little problem...

I have drop down lists from cell ranges I9:NI39 and need to be able to drag on them. I already formatted the cell and unchecked the Hidden and locked boxes, however when I protect the sheet I cannot drag on those cells. Is there a Macro I can add to the above one to help with this problem?


Thanks again in advance!
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,927
Members
449,094
Latest member
teemeren

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