Consulting

Results 1 to 5 of 5

Thread: Find a string and clear cell's contents to the right

  1. #1
    VBAX Regular
    Joined
    Oct 2014
    Posts
    24
    Location

    Post Find a string and clear cell's contents to the right

    Hi everyone,

    I have successfully created a macro that searches for a specific string (for example, "EEOG"), and clears the contents of the cell 3 columns to the right of said string. My question is- how can I get my code to find ALL of said strings, and clear the contents of the cell 3 columns to the right, throughout my entire workbook? Right now it only finds 1 row that contains "EEOG" but not all the other rows.

    Here is my working code:

    Sub Macro1()
    
        Cells.Find(What:="EEOG", LookIn:=xlFormulas, LookAt _
            :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            True, SearchFormat:=False).Offset(, 3).Select
        Selection.ClearContents
    
    End Sub
    Thanks
    -Johnathan

  2. #2
    VBAX Regular
    Joined
    Oct 2014
    Posts
    24
    Location
    Solved! Ended up using :

    Dim xRange As Range 
    Dim xCel As Range
       Set xRange = Range("A:A")
        For Each xCel In xRange
            If xCel.Value = "EEOG" Then
                xCel.Offset(, 3).ClearContents
            End If
        Next xCel

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    reading:
    https://msdn.microsoft.com/en-us/lib.../ff839746.aspx

    and adopting:
    Sub vbax_54785_Find_All_Occurences_Of_SearchString()
    
    
        Dim FoundCell As Range
        Dim FirstAddress As String
        
        With ActiveSheet.UsedRange.Cells
            Set FoundCell = .Find(What:="EEOG", LookIn:=xlFormulas, MatchCase:=True)
            If Not FoundCell Is Nothing Then
                FirstAddress = FoundCell.Address
                Do
                    FoundCell.Offset(, 3).ClearContents
                    Set FoundCell = .FindNext(FoundCell)
                Loop While Not FoundCell Is Nothing And FoundCell.Address <> FirstAddress
            End If
        End With
    
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    Without looping.
    Might be faster. Amount of data dependent.


    Sub test()
    Application.ScreenUpdating = False
        Rows(1).Insert
        With Columns("A")
            .AutoFilter 1, "EEOG"
            .Offset(, 3).ClearContents
            .AutoFilter
        End With
        Rows(1).Delete
    Application.ScreenUpdating = True
    End Sub

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Even simpler/faster

    Sub M_snb()
       [D1:D2000]=[if(A1:A2000="","",if(A1:A2000="EEOG","",D1:D2000))]
    End sub

Posting Permissions

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