0

I'm trying to use VBA in a macro to search for a text string and delete the contents of the column. I previously found this on the website and would like to change it to search columns and delete the text "QA1" while retaining the columns. I hope this makes sense.

LastRow = Cells(Columns.Count, "D").End(xlUp).Row

For i = LastRow To 1 Step -1
   If Range("D" & i).Value = "D" Then
      Range("D" & i).EntireColumn.Delete
   End If
Next i
1
  • Have you tried out any modifications to the example code? What do you think each line is doing? Commented Jul 13, 2016 at 13:06

3 Answers 3

1

You want to clear the contents of the whole column if one cell contains QA1?

Sub Test()

    Dim rCell As Range

    With ThisWorkbook.Worksheets("Sheet1").Columns(4)
        Set rCell = .Find("QA1", LookIn:=xlValues)
        If Not rCell Is Nothing Then
            .ClearContents
        End If
    End With

End Sub

If you want to just clear each instance of QA1 in column D:

Sub Test()

    Dim rCell As Range

    With ThisWorkbook.Worksheets("Sheet1").Columns(4)
        Set rCell = .Find("QA1", LookIn:=xlValues)
        If Not rCell Is Nothing Then
            Do
                rCell.ClearContents
                Set rCell = .FindNext(rCell)
            Loop While Not rCell Is Nothing
        End If
    End With

End Sub

Can it be written to look through the entire worksheet and delete QA1 where ever it is found?

All instances of QA1 on sheet:

Sub Test()

    Dim rCell As Range

    With ThisWorkbook.Worksheets("Sheet1").Cells
        Set rCell = .Find("QA1", LookIn:=xlValues)
        If Not rCell Is Nothing Then
            Do
                rCell.ClearContents
                Set rCell = .FindNext(rCell)
            Loop While Not rCell Is Nothing
        End If
    End With

End Sub

Edit: Add LookAt:=xlWhole to the Find arguments so it doesn't delete cells containing QA1 and other text (e.g. QA11 or Some text QA1)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this was what I was looking for. I'm still in the process of learning VBA and macros so this is very appreciated. Now to decipher what you wrote. Thanks again.
I'd suggest checking these links: msdn.microsoft.com/en-us/library/wc500chb.aspx , msdn.microsoft.com/en-us/library/office/ff839746.aspx Feel free to mark as accepted answer if it is.
0

This code goes through columns in a specified row and removes the "QA1" if found

Dim LastColumn As Integer
Dim RowNumber As Integer
Dim i As Integer

LastColumn = UsedRange.SpecialCells(xlCellTypeLastCell).Column
RowNumber = 1 'Adjust to your needs

For i = 1 To LastColumn Step 1

      Cells(RowNumber, i).Value = Replace(Cells(RowNumber, i).Value, "QA1", "")
Next i

2 Comments

Can it be written to look through the entire worksheet and delete QA1 where ever it is found?
see jcarroll's answer
0

Loops through the used range of the active worksheet, and removes the selected text.

Sub RemoveText()

    Dim c As Range
    Dim removeStr As String

    removeStr = InputBox("Please enter the text to remove")

    For Each c In ActiveSheet.UsedRange
        If c.Value = removeStr Then c.Delete
    Next c

End Sub

1 Comment

This worked but since I have a lot of rows and columns it took a great deal of time for Excel to execute. I plan to keep it all the same for future use. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.