How to make Excel VBA ignore formula cells with if condition

I want to use a condition ifto complete the task ClearContents. Please find my code below for this. I wrote below 2 codes, but both of them were unsuccessful.

  • Try first

    t = 1
    Do While Cells(t, 1) <> ""
        If Cells(t, 1) <> "=" Then
            Cells(t, 1).ClearContents
        End If
        t = t + 1
    Loop
    
  • Second attempt

    t = 1
    Do While Cells(t, 1) <> ""
        If Cells(t, 1) <> Formula Then
            Cells(t, 1).ClearContents
        End If
        t = t + 1
    Loop
    

Basically, I don’t want to delete cells containing the available formulas, but I want to delete other data.

+4
source share
2 answers

Write something like this:

If Not Cells(t,1).HasFormula Then

End if

This will work. Or try this:

Sub TestMe()

    If Not Cells(1, 1).HasFormula Then
        Debug.Print "No Formula"
    Else
        Debug.Print "Has Formula"
    End If

End Sub

Here is additional property information .HasFormula:

https://msdn.microsoft.com/en-us/library/office/ff837123.aspx

+1
source

, () .

Cells.SpecialCells(xlCellTypeConstants, 23).ClearContents

, .

, Cells Range("A1:A100") Selection.

( Excel Home → Editing → Find and Select → Go to Special. Excel .)

+4

Source: https://habr.com/ru/post/1678771/


All Articles