400 Error when excel cells are protected

DemonX

New Member
Joined
Jan 17, 2021
Messages
19
Office Version
  1. 2019
Platform
  1. Windows
I used the following code to check the duplicate records at A2 cell and last one to refresh the A2 cell. Now, when I protected the sheet ( any other cell without A2 cell too ) I get 400 error.


Sub bTest()
'A BETTER VERSION
Dim spl As Variant, rCell As Range
Dim sBaseText As String, sToHighlight As String
Dim i As Long, j As Long, lCount As Long, pos As Long
Dim dicColors As Object

For Each rCell In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
'Initialize variables
sBaseText = rCell.Value
Set dicColors = CreateObject("Scripting.Dictionary")
dicColors.CompareMode = vbTextCompare

'Split cell and loop
spl = Split(sBaseText, ";")
For i = LBound(spl) To UBound(spl)
If Len(spl(i)) > 0 Then
lCount = (Len(sBaseText) - Len(Replace(sBaseText, spl(i), ""))) / Len(spl(i))
'Check if it's a duplicate
If lCount > 1 Then
If Not dicColors.exists(spl(i)) Then
dicColors(spl(i)) = dicColors.Count + 1
sToHighlight = spl(i)
pos = 0
For j = 1 To lCount
pos = InStr(pos + 1, sBaseText, spl(i), vbTextCompare)
rCell.Characters(Start:=pos, Length:=Len(spl(i))). _
Font.ColorIndex = 2 + dicColors(spl(i))
Next j
End If
End If
End If
Next i
Next rCell
End Sub


Sub Clearcells()
'Updateby Extendoffice
Range("A2:A7").ClearContents

End Sub

Do I need to do any change to code ?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,090
Office Version
  1. 2016
Platform
  1. Windows
You can't change cells with VBA if cells are protected.
You need to unprotect the cells you want to change by a macro or you unprotect the sheet in the macro.
 

DemonX

New Member
Joined
Jan 17, 2021
Messages
19
Office Version
  1. 2019
Platform
  1. Windows
Hey Mart37. Thanks for replying. Here my concern is, I can not protect cells not covered by VBA also. my guess was error in VBA may be ?
 

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,090
Office Version
  1. 2016
Platform
  1. Windows
Yes That is possible. When the sheet is unprotected does the macro work?
 

DemonX

New Member
Joined
Jan 17, 2021
Messages
19
Office Version
  1. 2019
Platform
  1. Windows
Yes it works well when nothing is protected.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,377
Messages
5,635,896
Members
416,887
Latest member
SheriE

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
Top