locking and unlocking the excel cells using VBA

Partha_sai

New Member
Joined
Sep 20, 2014
Messages
1
Hi All,

I am new to VBA coding. My requirement is to unlock 3 columns when the user select update and to insert a fresh row when they select add. I am able to do that. by my excel has nearly 20,000 records and each and every time when some change occurs I am looping the whole sheet to lock and unlock which is almost taking more than 1 min.
Can any suggest if I can reduce the time its taking . Please find the code which I am using

Public Sub Worksheet_Activate_after()

Sheets("Sheet1").Unprotect
Sheets("Sheet1").Cells.Locked = False
For i = 1 To Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
If Cells(i, 1).Value = "Add" Then
Cells(i, 2).Locked = False

Cells(i, 3).Locked = False

Cells(i, 4).Locked = False

Cells(i, 5).Locked = False

Cells(i, 6).Locked = False

ElseIf Cells(i, 1).Value = "Update" Then
Cells(i, 2).Locked = True
Cells(i, 3).Locked = True
Cells(i, 4).Locked = False
Cells(i, 5).Locked = False
Cells(i, 6).Locked = False

Else
Cells(i, 4).Locked = True

Cells(i, 5).Locked = True

Cells(i, 6).Locked = True
Cells(i, 2).Locked = True
Cells(i, 3).Locked = True

End If

Next
Sheets("Sheet1").Protect
End

End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,214,918
Messages
6,122,249
Members
449,075
Latest member
staticfluids

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