How can I use VBA to delete all columns which are empty apart from a specific header?

前端 未结 2 1663
甜味超标
甜味超标 2021-01-27 04:43

I\'d like to delete all columns in a worksheet which meet the following criteria:

  • row 1 = \"foobar\"
  • rows 2-1000 are empty

It sounds simpl

2条回答
  •  广开言路
    2021-01-27 05:06

    How about

    dim col as Long, lastCol as Long, r as range
    lastCol = ActiveSheet.Usedrange.columns(Activesheet.Usedrange.columns.count).column
    for c=lastCol to 1 Step -1
        set r = Range(Cells(1, c), Cells(1000, c))
        if r.Rows(1) = "foobar" Then
            if WorksheetFunction.CountA(Range(r.Rows(2), r.Rows(r.Rows.Count))) = 0 then
                Columns(c).delete
            end if
        end If
    next
    

    [edit by OP: added a missing space]

提交回复
热议问题