Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default Unlock certain cells based on ColorIndex

I am working on a document that calculates indices based on a set of default
weightings. I want to allow the user to redistribute these weights without
changing the default information. It is set up so that the user can make
alterations in the cells that have a yellow background. There are many sheets
in the workbook and the location of the yellow cells are not uniform. I've
written the following code that loops through each worksheet, unlocks the
yellow cells, and protects the sheet. The trouble is that it is very buggy
and for some reason selects some yellow cells but not others. Occasionally I
get an error 1004 message. Is there a more elegant way to do this?


For Each wks In ActiveWorkbook.Worksheets
str = wks.Name

'Unlock yellow cells
For ColIndex = 1 To 40
For RowIndex = 1 To 320
If Cells(RowIndex, ColIndex).Interior.ColorIndex = 36 Then
Cells(RowIndex, ColIndex).Locked = False
Cells(RowIndex, ColIndex).FormulaHidden = False
End If

Next RowIndex
Next ColIndex

'Protect worksheet
wks.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

Next wks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Unlock certain cells based on ColorIndex

Have a go with this -

Sub UnLockClrIdx36()
Dim sPassWord As String
Dim rng As Range, cell As Range
Dim wb As Workbook
Dim ws As Worksheet

sPassWord = "" ' "abc"

Set wb = ActiveWorkbook
For Each ws In wb.Worksheets
With ws
.Unprotect sPassWord
Set rng = .Range(.Cells(1, 1), .Cells(320, 40))
Set rng = Intersect(.UsedRange, rng)
End With

For Each cell In rng
If cell.Interior.ColorIndex = 36 Then
With cell
.Locked = False
.FormulaHidden = False
End With
End If
Next

ws.Protect _
Password:=sPassWord, _
DrawingObjects:=True, _
Contents:=True, _
Scenarios:=True

Next

End Sub

Regards,
Peter T

"Samantha" wrote in message
...
I am working on a document that calculates indices based on a set of

default
weightings. I want to allow the user to redistribute these weights without
changing the default information. It is set up so that the user can make
alterations in the cells that have a yellow background. There are many

sheets
in the workbook and the location of the yellow cells are not uniform. I've
written the following code that loops through each worksheet, unlocks the
yellow cells, and protects the sheet. The trouble is that it is very buggy
and for some reason selects some yellow cells but not others. Occasionally

I
get an error 1004 message. Is there a more elegant way to do this?


For Each wks In ActiveWorkbook.Worksheets
str = wks.Name

'Unlock yellow cells
For ColIndex = 1 To 40
For RowIndex = 1 To 320
If Cells(RowIndex, ColIndex).Interior.ColorIndex = 36 Then
Cells(RowIndex, ColIndex).Locked = False
Cells(RowIndex, ColIndex).FormulaHidden = False
End If

Next RowIndex
Next ColIndex

'Protect worksheet
wks.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

Next wks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default Unlock certain cells based on ColorIndex

Peter,
This seems to be working okay. Thanks so much!

"Peter T" wrote:

Have a go with this -

Sub UnLockClrIdx36()
Dim sPassWord As String
Dim rng As Range, cell As Range
Dim wb As Workbook
Dim ws As Worksheet

sPassWord = "" ' "abc"

Set wb = ActiveWorkbook
For Each ws In wb.Worksheets
With ws
.Unprotect sPassWord
Set rng = .Range(.Cells(1, 1), .Cells(320, 40))
Set rng = Intersect(.UsedRange, rng)
End With

For Each cell In rng
If cell.Interior.ColorIndex = 36 Then
With cell
.Locked = False
.FormulaHidden = False
End With
End If
Next

ws.Protect _
Password:=sPassWord, _
DrawingObjects:=True, _
Contents:=True, _
Scenarios:=True

Next

End Sub

Regards,
Peter T

"Samantha" wrote in message
...
I am working on a document that calculates indices based on a set of

default
weightings. I want to allow the user to redistribute these weights without
changing the default information. It is set up so that the user can make
alterations in the cells that have a yellow background. There are many

sheets
in the workbook and the location of the yellow cells are not uniform. I've
written the following code that loops through each worksheet, unlocks the
yellow cells, and protects the sheet. The trouble is that it is very buggy
and for some reason selects some yellow cells but not others. Occasionally

I
get an error 1004 message. Is there a more elegant way to do this?


For Each wks In ActiveWorkbook.Worksheets
str = wks.Name

'Unlock yellow cells
For ColIndex = 1 To 40
For RowIndex = 1 To 320
If Cells(RowIndex, ColIndex).Interior.ColorIndex = 36 Then
Cells(RowIndex, ColIndex).Locked = False
Cells(RowIndex, ColIndex).FormulaHidden = False
End If

Next RowIndex
Next ColIndex

'Protect worksheet
wks.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

Next wks




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
unlock cell based on formula Huber57 Excel Discussion (Misc queries) 1 June 8th 07 04:39 PM
Insert Value based on ColorIndex, for specific range on all sheets Ray Excel Programming 3 March 20th 07 02:50 PM
How to get cell colorindex of conditional formatted cells [email protected] Excel Discussion (Misc queries) 5 December 2nd 06 01:07 PM
how do i password protect an .xls file? how do i unlock it for automation. e.g. want to unlock and access a .xls from another .xls macro. Daniel Excel Worksheet Functions 1 June 24th 05 02:59 PM
Is there away to have specific cells unlock based on the entry of information in another? Marc New Users to Excel 2 April 17th 05 06:09 PM


All times are GMT +1. The time now is 07:33 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"