VBA to lock cell and insert formula

cjkohler88

New Member
Joined
Jan 20, 2010
Messages
6
I am having some trouble getting my VBA to work. I am trying to unprotect a worksheet, set a cell to lock, insert a formula if the cell ="us" and then reprotect the cell. if the cell is not equal to "US" than I want to clear the cell. I can get the code to unlock, set the protection or unprotect, and then relock using the following code.

Private Sub Worksheet_Change(ByVal Target As Range)
If [c4] = "US" Then
ActiveSheet.Unprotect ("eXpense")
[c7].Locked = True
ActiveSheet.Protect ("eXpense")
'Remove locked property if B3's value is anything else or is deleted.
Else
ActiveSheet.Unprotect ("eXpense")
[c7].Locked = False
'Optional, reprotect sheet
ActiveSheet.Protect ("eXpense")
End If
End Sub

how do i get it to either insert a function or clear the cell. I have tried a lot of variations and it get an error or a loop every time
 

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
Hi and welcome to the board!!!
I don't really follow what you are asking! Explain in staright talk what you are doing and what you want to happen. Like
If I enter "US" in cell C4, then lock C4 etc.
whatever
I don't see where C7 and B3 are coming into play, so we need more info.
You are using a Change_Event. What cells or range do you want it to work with?
lenze
 

cjkohler88

New Member
Joined
Jan 20, 2010
Messages
6
sorry, B3 was a mistake, it should be C4. here is what I want to do. In cell C4 I have a drop down box that includes a bunch of countries. If soemone picks US I want it to unprotect the sheet, insert the function "=US_Mile_Rate" into c7, lock the cell, and then reprotect the sheet. if they choose any other option, I want it to unprotect the sheet, clear the value in c7, and reprotect the sheet. right now all i can get it to do is unprotect the sheet, set the cell as locked or unlocked and reprotect the worksheet. does that help?
 

cjkohler88

New Member
Joined
Jan 20, 2010
Messages
6
here is my updated code:

Private Sub Worksheet_Change(ByVal Target As Range)
If [c4] = "US" Then
ActiveSheet.Unprotect ("eXpense")
[c7].Locked = True
ActiveSheet.Protect ("eXpense")
Else
ActiveSheet.Unprotect ("eXpense")
[c7].Locked = False
ActiveSheet.Protect ("eXpense")
End If
End Sub
 

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
What is "=US_Mile_Rate"
A formula? A Named Range? Text?

lenze
 

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$C$4" Then Exit Sub
ActiveSheet.Unprotect "eXpense"
If Target <> "US" Then
    Range("$C$7").ClearContents
Else
    Range("$C$7") = Range("US_Mile_Rate")
    Range("$A$7").Locked = True
End If
ActiveSheet.Protect "eXpense"
End Sub
I assume the Named range is on the same sheet. If not you may need to reference the sheet it is on.
lenze
 

Forum statistics

Threads
1,081,899
Messages
5,361,928
Members
400,666
Latest member
UDLearning

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top