How to select and delete every 3rd column

后端 未结 3 1618
长发绾君心
长发绾君心 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

    Reverse the direction. Start deleting from the right. I think you know how to modify your code

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-14 16:16

    You can go backwards like this. Also, you don't need to select the column before deleting, you can simply delete it right away.

    For i = ((LastColumn \ 4) * 4) To 4 Step -4
       ws.Columns(i).Delete Shift:=xlToLeft
    Next i
    
    0 讨论(0)
提交回复
热议问题