allow macro to add data to a protected cell

dsrt16

Board Regular
Joined
Jun 18, 2005
Messages
208
I have a loop code which enters a formula into each worksheet 3-31 in cell A17.

However, I need to protect cell A17, so no user enters data or deletes the results of the formula on accident.

I know I how to write a macro to allow this through the context of setting a certain worksheet.
Code:
Dim Wks As Worksheet

    Set Wks = Worksheets("My Data")

Wks.UnProtect Password:="password here"

But I don't know to do it in context of my current loop code:
The code
Code:
Dim i As Integer

    For i = 3 To 31
        
        Sheets(i).Range("A17").FormulaR1C1 = "=PULL(""'X:\Staff\AI Program\""&'Set-up Page'!R[-11]C[4]&""\[""&'Set-up Page'!R[-11]C[5]&""]""&R[-15]C[2]&""'!A17"")"
       

    Next i

Cell A17 needs to return the results of a formula, but it needs to be protected.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
if i understand you
you need to loop throw all sheets
try this
Code:
Sub Test()
Dim Wks As Worksheet
Dim i As Integer

For Each Wks In Sheets

Wks.Unprotect Password:="password here"
For i = 3 To 31
     Sheets(i).Range("A17").FormulaR1C1 = "=PULL(""'X:\Staff\AI Program\""&'Set-up Page'!R[-11]C[4]&""\[""&'Set-up Page'!R[-11]C[5]&""]""&R[-15]C[2]&""'!A17"")"
Next i

Wks.Protect Password:="password here"
Next Wks
End Sub
 
Upvote 0
Yahya

You could avoid the double loop in the code, like so..

Code:
Sub Test()
Dim i As Integer
For i = 3 To 31
     With Sheets(i)
        .Unprotect Password:="password here"
        .Range("A17").FormulaR1C1 = "=PULL(""'X:\Staff\AI Program\""&'Set-up Page'!R[-11]C[4]&""\[""&'Set-up Page'!R[-11]C[5]&""]""&R[-15]C[2]&""'!A17"")"
        .Protect Password:="password here"
    End With
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,700
Members
452,938
Latest member
babeneker

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