Results 1 to 3 of 3

Thread: VBA Clear Cell if

  1. #1
    Board Regular
    Join Date
    Dec 2018
    Posts
    51
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default VBA Clear Cell if

    Hello,

    I'm new to VBA and need some help. Im bascially trying to say - look in column C, if the value is "State ID" then clear contents


    Sub ClearRowWith_ID()
    Last = Cells(Rows.Count, "C").End(xlUp).Row
    For i = Last To 1 Step -1
    If (Cells(i, "C").Value) = "State ID" Then

    End If
    Next i
    End Sub

  2. #2
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    29,229
    Post Thanks / Like
    Mentioned
    483 Post(s)
    Tagged
    49 Thread(s)

    Default Re: VBA Clear Cell if

    No need for a loop.
    Code:
    Sub Sparda142()
       Range("C:C").Replace "State ID", "", xlWhole, , False, , False, False
    End Sub
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  3. #3
    MrExcel MVP
    Junior Admin
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    51,060
    Post Thanks / Like
    Mentioned
    57 Post(s)
    Tagged
    11 Thread(s)

    Default Re: VBA Clear Cell if

    Try:
    Code:
    Sub ClearRowWith_ID() Last = Cells(Rows.Count, "C").End(xlUp).Row For i = 1 to Last If Cells(i, "C").Value = "State ID" Then Cells(i, "C").ClearContents Next i End Sub
    Note that you do not have to iterate threw the rows backwards if you are just clearing cells and not deleting rows.


    EDIT: Fluff's way is more efficient/faster, as it does not use loops.
    Last edited by Joe4; Aug 5th, 2019 at 11:29 AM.
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •