How to select and delete every 3rd column

后端 未结 3 1617
长发绾君心
长发绾君心 2021-01-14 15:56

I have a set of data where every third column is the same. I want to leave only the first column and other which are the same must be deleted.

At first I tried this

3条回答
  •  北海茫月
    2021-01-14 16:10

    I upvoted Mattboy's code as the cleanest

    It is possible to avoid the range loop and use an array as you suggest, although I post this more for kicks as the array generation is tricky

    Uses Is it possible to fill an array with row numbers which match a certain criteria without looping?

    Sub OneinThree()
    Dim ws As Worksheet
    Dim rng1 As Range
    Dim x As String
    Set ws = ActiveSheet
    Set rng1 = Cells(1, ws.Cells(1, Columns.Count).End(xlToLeft).Column - 2)
    x = Join(Filter(Application.Evaluate("=IF(MOD(column(A1:" & rng1.Address & "),3)=0,address(1,column(a1:" & rng1.Address & ")),""x"")"), "x", False), ",")
    If Len(x) > 0 Then ws.Range(x).EntireColumn.Delete
    End Sub
    

提交回复
热议问题