This code should be located in the codepart of sheet1
NOT in a module
put anything in te cells a1 and a2
when you change to sheet2 and then back, see what happens.
To reset click Alt-F8 and execute RESET
****.
Code:
Sub LockColumns()
Application.ScreenUpdating = True
With Application.Sheets(1)
.Unprotect
.Activate
.Cells(1, 1).Select
Do While Selection.Value <> "" 'If selection is not empty
Selection.EntireColumn.Interior.Color = 65535
Selection.EntireColumn.Locked = True
Selection.Offset(0, 1).Select
Loop
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
End Sub
Sub Reset()
Sheets("Blad1").Select
Sheets("Blad1").Unprotect
Cells.Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Selection.Locked = False
Selection.FormulaHidden = False
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Sheets("Blad1").Range("A1").Select
End Sub
Private Sub Worksheet_Activate()
LockColumns
End Sub
_---------------------------------------------------------------------------------------------------
Hello ****,
I tried the above code, but it is coloring the whole sheet 1, but not the cells with data. And i want to present my problem more clearly....As below
1. As i opens this sheet every time the data from Row # 2 to the last row containg the data must locked. (In this case- From teh row 2 to row 4 has to be locked).
2.Row1 will always be empty, bcas i need to enter the data in that row and click on "Add data" command button. Then teh data will be added at the last empty available empty row in teh sheet. In teh below i have entered teh new data "Walsh, 111" and click on the Add data button teh data will be added in the last empty row.
Before adding the data [TABLE="width: 192"]
<COLGROUP><COL style="WIDTH: 48pt" span=4 width=64><TBODY>[TR]
[TD="class: xl65, width: 64, bgcolor: yellow"]
Walsh[/TD]
[TD="class: xl65, width: 64, bgcolor: yellow, align: right"]
111[/TD]
[TD="class: xl66, width: 64, bgcolor: #00b050"]
Add Data[/TD]
[TD="class: xl67, width: 64, bgcolor: transparent"] [/TD]
[/TR]
[TR]
[TD="class: xl67, bgcolor: transparent"]
Name[/TD]
[TD="class: xl67, bgcolor: transparent"]
Number[/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[/TR]
[TR]
[TD="class: xl67, bgcolor: transparent"]
Hari[/TD]
[TD="class: xl67, bgcolor: transparent, align: right"]
11[/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[/TR]
[TR]
[TD="class: xl67, bgcolor: transparent"]
Raj[/TD]
[TD="class: xl67, bgcolor: transparent, align: right"]
99[/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[/TR]
[TR]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[/TR]
</TBODY>[/TABLE]
AFter Adding the Data
[TABLE="width: 192"]
<COLGROUP><COL style="WIDTH: 48pt" span=4 width=64><TBODY>[TR]
[TD="class: xl65, width: 64, bgcolor: yellow"] [/TD]
[TD="class: xl65, width: 64, bgcolor: yellow"] [/TD]
[TD="class: xl66, width: 64, bgcolor: #00b050"]
Add Data[/TD]
[TD="class: xl67, width: 64, bgcolor: transparent"] [/TD]
[/TR]
[TR]
[TD="class: xl67, bgcolor: transparent"]
Name[/TD]
[TD="class: xl67, bgcolor: transparent"]
Number[/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[/TR]
[TR]
[TD="class: xl67, bgcolor: transparent"]
Hari[/TD]
[TD="class: xl67, bgcolor: transparent, align: right"]
11[/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[/TR]
[TR]
[TD="class: xl67, bgcolor: transparent"]
Raj[/TD]
[TD="class: xl67, bgcolor: transparent, align: right"]
99[/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[/TR]
[TR]
[TD="class: xl67, bgcolor: transparent"]
Walsh[/TD]
[TD="class: xl67, bgcolor: transparent, align: right"]
111[/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[TD="class: xl67, bgcolor: transparent"] [/TD]
[/TR]
</TBODY>[/TABLE]
3. Now the data from Row 2 to row that contains the data i.e. Row # 5 has to be locked. I mean only the cells with data has to be locked always, even every time when i opens the sheet. (I mean immedietly when i opens the work sheet/workbook the data from Row to last row containing data has to be locked)...
I have macro now for adding the data at the bottom , but i need to have a macro only for locking the cells that contains the data always and that macro need to lock all the available data from row #2)
And if I want to edit any changes in the data from row # 2 it should ask me for a password.
Regards
Harikrishna