Clear Range Without Formulas & Bold Text

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Clear Range Without Formulas & Bold Text

Post by adam »

Hi anyone,

The following code clears data in column G and cell K9 within the specified range. How could the code be modified so that it even limits clearing the formulas in column G?

Code: Select all

Sub ClearReport()
Dim wshS As Worksheet
    Dim wshT As Worksheet
Dim s As Long
    Dim m As Long
Application.ScreenUpdating = False
    Application.EnableEvents = False
    ' Source sheet
    Set wshS = Worksheets("Report")
    ' Get last row
    m = wshS.Range("G:G").Find(What:="THANK YOU").Row
    For s = 15 To m - 1
        If wshS.Range("G" & s).Value <> "Results" Then
            wshS.Range("G" & s).ClearContents
            Worksheets("Report").Range("K9").ClearContents
        End If
    Next s
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Any help on this would be kindly appreciated.

Thanks in advance.
Best Regards,
Adam

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Clear Range Without Formulas & Bold Text

Post by Rudi »

Hi Adam,

You can try this code...

Code: Select all

Sub ClearReport()
Dim wshS As Worksheet
Dim wshT As Worksheet
Dim s As Long
Dim m As Long
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    ' Source sheet
    Set wshS = Worksheets("Report")
    ' Get last row
    m = wshS.Range("G:G").Find(What:="THANK YOU").Row
    Worksheets("Report").Range("K9").ClearContents
    For s = 15 To m Step -1
        If wshS.Range("G" & s).Value <> "Results" And _
            Not wshS.Range("G" & s).HasFormula Then
            wshS.Range("G" & s).ClearContents
        End If
    Next s
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Last edited by Rudi on 24 Apr 2014, 06:17, edited 1 time in total.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Clear Range Without Formulas & Bold Text

Post by HansV »

Instead of

Mid(wshS.Range("G" & s).Formula, 1, 1) <> "="

you could use

Not Mid(wshS.Range("G" & s).HasFormula

(the original condition could be True if the cell has a text value beginning with an =.)
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Clear Range Without Formulas & Bold Text

Post by Rudi »

TX.
I've updated the code to use your suggestion, edited to: Not wshS.Range("G" & s).HasFormula :cheers:
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Clear Range Without Formulas & Bold Text

Post by adam »

I'm sorry for the late reply. But the code does not seem to clear the column G even though it does clear the cell K9. The formats of cells in column G are in "Text" format.

Is this the reason why it's not working?
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Clear Range Without Formulas & Bold Text

Post by HansV »

Change the line

Code: Select all

    For s = 15 To m Step -1
in Rudi's macro to

Code: Select all

    For s = 15 To m - 1
as in your original version.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Clear Range Without Formulas & Bold Text

Post by adam »

Thanks for the help Hans. It works fine now.
Best Regards,
Adam