简体   繁体   中英

Macro VBA find specific column

I would need to find a specific word on row 3 such as Budget and Status after that, I would need to delete the entire column which contains Status and Budget.

PS. can be more words and how to make it go to the last column and find those specific words

在此处输入图片说明

This simple code will loop through row3 backwards and if it finds the values you are looking for it will delete the column (Note: you have to loop backwards from the last column). You can add more words by copying Or .Cells(3, i).Value = "Budget" and pasting it in front of Next , then change the word "Budget" as needed.

Dim lCol As Long, i As Long

With ThisWorkbook.Sheets("Sheet1")
    lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

    For i = lCol To 2 Step -1
        If .Cells(3, i).Value = "Status" Or .Cells(3, i).Value = "Budget" Then
            .Cells(3, i).EntireColumn.Delete
        End If
    Next i

End With

You can search for single words and delete columns quickly and simply with 1 line of code:

Cells.Find(What:="Budget", After:=Range("A3"), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows).EntireColumn.Delete

This line finds What you want starting from cell A3 and searches the SearchOrder is by row.

If you ever want to find more words, just copy and paste the code and change the What

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM