简体   繁体   中英

Deleting A Column when a row contains a certain value

I have a pretty big excel file that has week ending sums for each week. I want to remove the week ending columns. I am trying to write this in VBA so that I can apply this to multiple worksheets as I have to do this fairly often.

All of the weekending columns have "W/E" in the 3rd row, and I want to write the code to delete any column that contains "W/E" in row 3.

I know that there have been answers to deleting rows that contain values in columns but I was not able to convert those codes to be the other way around (deleting columns based on value in a certain row).

Thanks in advance for the help!

dim c as range, rngDel as range

for each c in activesheet.range("A3:Z3").cells
    if instr(c.value, "W/E")>0 then
         if rngDel is nothing then
            set rngDel = c
         else
            set rngDel = application.union(rngDel, c)
         end if
    end if
next c

if not rngDel is nothing then rngDel.entirecolumn.delete

蛮粗糙的,但这可能有用

Columns(WorksheetFunction.match("W/E", rows(3), 0)).Delete

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