Consulting

Results 1 to 5 of 5

Thread: Solved: delete columns with an array collection

  1. #1
    VBAX Expert mperrah's Avatar
    Joined
    Mar 2005
    Posts
    744
    Location

    Solved: delete columns with an array collection

    I was trying to clean up some recorded macros and found this method promising, not sure how it works.
    Can I delete several columns at once?
    - whats the format for the procedure?
    below I have the columns I am deleting from right to left
    the end of each line is what I calculated the total number of columns for each contiguous range.


    [vba]Sub test_delete_raw()
    Dim Arr
    Arr = Array(1, 3, 5, 7, 9) ' 1:2, 4:5, 7:8, 10, 15, 20:44, 46:53, 55:256?
    'or right to left - 256:55, 53:46, 44:20, 15, 10, 8:7, 5:4, 2:1
    For i = 256 To 0 Step -1
    Columns(Arr(i)).Delete
    Next
    End Sub[/vba]

    column name - total cols - from "A" - (Range from col 256)

    Columns("BC:IV").Select - 202 - 54 - (55-256)
    Columns("AT:BA").Select 8 - 46 - (46-53)
    Columns("T:AR").Select - 25 - 21 - (20-44)
    Columns("O:O").Select - 1 - 20 - (15)
    Columns("J:J").Select - 1 - 19 - (10)
    Columns("G:H").Select - 2 - 17 - (7-8)
    Columns("D:E").Select - 2 - 15 - (4-5)
    Columns("A:B").Select - 2 - 13 - (1-2)

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub test_delete_raw()
    Dim Arr
    Dim i As Long
    Arr = Array("A:B", "D:E", "G:H", "J:J", "T:AR", "AT:BA", "BC:IV")
    For i = UBound(Arr) To LBound(Arr) Step -1
    Columns(Arr(i)).Delete
    Next
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Just for information, you don't actually need the array for this - you can use something like:
    [VBA]Range("A:B,D:E,G:H,J:J,T:AR,AT:BA,BC:IV").EntireColumn.Delete[/VBA]

    FWIW.
    Rory

  4. #4
    VBAX Expert mperrah's Avatar
    Joined
    Mar 2005
    Posts
    744
    Location
    Does this shift the remaining columns left after deleting?
    So when the old columns are gone there will be 13 columns left
    with out spaces?
    Mark

  5. #5
    VBAX Expert mperrah's Avatar
    Joined
    Mar 2005
    Posts
    744
    Location
    Got it, Thanks
    [vba] Rows("1:1").Delete
    Range("A:B,D:E,G:H,J:J,O:O,T:AR,AT:BA,BC:IV").EntireColumn.Delete[/vba] OMG that is sweet!
    Thank you thank you!
    Mark

    [VBA]sub thanks()
    Dim help as variant

    With help
    .yourock
    End With ' never

    End sub[/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •