VBA to clear columns but not protected cells

oblix

Board Regular
Joined
Mar 29, 2017
Messages
183
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
  9. 2003 or older
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi there want to create vba to clear cells on spread sheet
In columns G, and I to AM
The problem is that there are protected cells I dont want to clear
I tried something like this in column h to test but it also clears the protected cells

Sub EmptyStockCount()

On Error Resume Next
Sheets("Blank Stock Sheet").Columns("h:h").Value = vbNullString
On Error GoTo 0

End Sub
Dont understand what I amdoing Wrong
please some assist
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi, a VBA demonstration as a beginner starter :​
VBA Code:
Sub Demo1()
         Dim Rg As Range
    With Application
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    With Sheets("Blank Stock Sheet")
        For Each Rg In .Range(Replace("G1:G#,I1:AM#", "#", .UsedRange.Rows(.UsedRange.Rows.Count).Row))
          If Not Rg.Locked Then Rg.ClearContents
        Next
    End With
        .Calculation = xlCalculationAutomatic
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub
 
Upvote 0
Thank you for reply
I tried this but I forgot to mention that column g has merged cells
Therefore the code gives error
merged like g9&g10, g11&g12 and so it goes on
Is there a solution?
 
Upvote 0
As far as I can tell if you make this change it should work.
(Added ".MergeArea")

Change this:
VBA Code:
If Not Rg.Locked Then Rg.ClearContents

To this:
VBA Code:
If Not Rg.Locked Then Rg.MergeArea.ClearContents
 
Upvote 0
Thank you to both Marc L and Alex Blandenburg. Between the two of you my problem is solved.
Solution working fine
 
Upvote 0
Thanks for letting us know. Glad we were able to help and that @Marc L’s solution is now working for you.
 
Upvote 0
Does clearing 650 lines take long....seems to take long to clear cells.
 
Upvote 0
See if my non-looping method of doing what you asked for is any faster...
VBA Code:
Sub Test()
  Application.FindFormat.Clear
  Application.FindFormat.Locked = False
  Range("G:G", "I:AM").Replace "", "", SearchFormat:=True, ReplaceFormat:=False
  Application.FindFormat.Clear
End Sub
 
Upvote 0
Maybe I am missing something Rick..I tried but all cells are still filled.
Also dont see that it specifically only clears those fields in the "Blank Stock Sheet"
 
Upvote 0
Sorry, I missed that you were not going to run the code from the active sheet. See if this works for you now...
VBA Code:
Sub Test()
  Application.FindFormat.Clear
  Application.FindFormat.Locked = False
  Sheets("Blank Stock Sheet").Range("G:G", "I:M").Replace "", "", SearchFormat:=True, ReplaceFormat:=False
  Application.FindFormat.Clear
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,938
Messages
6,122,346
Members
449,080
Latest member
Armadillos

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