Project Sheet based on condition

grlinks

New Member
Joined
Jul 25, 2011
Messages
5
Hi friends

i am having time sheet workbook, this work book has around 30 sheets and each sheets named as date format(like 02-08-2011,03-08-2011) now i need to protect sheets which based on following conditions

1. user should not do any modification previous day work sheet (ie:- if today date is 04-08-2011 then previous all days sheet should be protected)

I've write code for this , its protecting all the sheets , not based on my conditions ,


Sub protectsheets()
Dim wSheet As Worksheet

For Each wSheet In Worksheets

If Format(Now, "dd-mm-yyyy") < Format(CDate(wSheet.Name), "dd-mm-yyyy") Then
wSheet.Protect Password:="Rhy77"
Else
wSheet.Unprotect Password:="Rhy77"
End If
Next wSheet
End Sub

Kindly help me
 
Last edited:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Welcome to the board..

There's 2 problems with the code...

1. The format function returns TEXT strings, so trying to compare one text string Greater than or less than another text string isn't really valid..

2. The logic you have is actually testing if the name of the sheet is in the FUTURE..
If Now < Sheet's Date
Should be
If Sheet's Date < Now

Also, less important...
Now contains both Date and Time.
You can use the VBA function DATE to test for just the current date..


Try

If DateValue(wSheet.Name) < Date Then


Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,909
Members
452,949
Latest member
beartooth91

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