Macro to lock string in cell

msoutopico

New Member
Joined
Jan 4, 2013
Messages
5
Hi there,

I have an Excel file with several cells containing text. The text contains some codes and I would like to know whether it's possible to lock those codes (by means of some formula or VBA macro, for example). The aim is that any text contained in the cell can be modified except those codes.

I know a whole cell can be locked/protected but I don't think it is possible to lock only part of the content of the cell. Am I right?

The codes could be matched with some regexp pattern or wildcard. E.g. The cell's content is, say: "Blabla some text [ABC_123] blabla more text blabla." I would like to lock any text being matched by regexp \[.+?\].

Thank you very much for your answers in advance! :)

Cheers, Manuel
 
The following code should correctly lock the codes within the text in Column F for the "[[T..]]" and "(Q..)" types of code.

Rich (BB code):
Dim CodeText As String, OriginalText As String

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Count > 1 Then Exit Sub
  If Not Intersect(Target, Columns("F")) Is Nothing Then
    If CodeText <> "" Then
      If Target.Value = "Delete=Okay" Then
        Application.EnableEvents = False
        Target.ClearContents
        Application.EnableEvents = True
      ElseIf CodeText <> Codes(Target.Text) Then
        MsgBox "You tried change a code value which is not allowed!"
        Application.EnableEvents = False
        Target.Value = OriginalText
        Application.EnableEvents = True
      End If
    End If
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Count > 1 Then Exit Sub
  If Not Intersect(Target, Columns("F")) Is Nothing Then
    OriginalText = Target.Value
    CodeText = Codes(Target.Value)
  End If
End Sub

Function Codes(ByVal S As String) As String
  Dim X As Long, CellText As String, Parts() As String
  ' [[T...]]
  CellText = Replace(S, "[[", "]]")
  Parts = Split(CellText, "]]")
  For X = 1 To UBound(Parts) Step 2
    If Parts(X) Like "T*" Then Codes = Codes & "[[" & Parts(X) & "]] "
  Next
  '  (Q...)
  CellText = Replace(S, ")", "(")
  Parts = Split(CellText, "(")
  For X = 1 To UBound(Parts) Step 2
    If Parts(X) Like "Q*" Then Codes = Codes & "(" & Parts(X) & ") "
  Next
End Function

Of course, I forgot to change the reference from Column A (where I originally had assumed your data was) to Column F where you now told us it is located. The two changes shown in red above are the changes that need to be made to address the correct column for your data.
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
As for the other codes, given they can be changed within their delimiters, as long as the delimiters are not changed... that is different from barring an outright change in any part of the code, so I'll have to think about that a little more in order to determine how implement it.
Okay, I have thought about it and I believe the following event code will handle all the possible codes meeting the restrictions you set out for us in Message #9....

Code:
Dim CodeText As String, OriginalText As String

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Count > 1 Then Exit Sub
  If Not Intersect(Target, Columns("F")) Is Nothing Then
    If CodeText <> "" Then
      If Target.Value = "Delete=Okay" Then
        Application.EnableEvents = False
        Target.ClearContents
        Application.EnableEvents = True
      ElseIf CodeText <> Codes(Target.Text) Then
        MsgBox "You tried change a code value which is not allowed!"
        Application.EnableEvents = False
        Target.Value = OriginalText
        Application.EnableEvents = True
      End If
    End If
  End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Count > 1 Then Exit Sub
  If Not Intersect(Target, Columns("F")) Is Nothing Then
    OriginalText = Target.Value
    CodeText = Codes(Target.Value)
  End If
End Sub

Function Codes(ByVal S As String) As String
  Dim X As Long, CellText As String, Parts() As String
  ' [[T...]]
  CellText = Replace(S, "[[", "]]")
  Parts = Split(CellText, "]]")
  For X = 1 To UBound(Parts) Step 2
    If Parts(X) Like "T*" Then Codes = Codes & "[[" & Parts(X) & "]] "
  Next
  '  (Q...)
  CellText = Replace(S, ")", "(")
  Parts = Split(CellText, "(")
  For X = 1 To UBound(Parts) Step 2
    If Parts(X) Like "Q*" Then Codes = Codes & "(" & Parts(X) & ") "
  Next
  '  {{..}}
  CellText = Replace(S, "{{", "}}")
  Parts = Split(CellText, "}}")
  For X = 1 To UBound(Parts) Step 2
    If Parts(X) Like "*" Then Codes = Codes & "{{*}} "
  Next
  '  <..>
  CellText = Replace(S, ">", "<")
  Parts = Split(CellText, "<")
  For X = 1 To UBound(Parts) Step 2
    If Parts(X) Like "*" Then Codes = Codes & "<*> "
  Next
End Function
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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