Consulting

Results 1 to 5 of 5

Thread: Select Multiple Columns using Column Number

  1. #1
    Banned VBAX Contributor Cosmos75's Avatar
    Joined
    May 2004
    Location
    Alabama, USA
    Posts
    118
    Location

    Select Multiple Columns using Column Number

    Is there a way to select multiple columns in VBA using the column number?

    I have this

    Range("D:E,G:H,J:K,M:N").Select 
    Selection.Delete Shift:=xlToLeft

    But I would prefer to be able to select those columns using column numbers. It will not always be the same columns so using column numbers would be easier for me to code.

  2. #2
    Banned VBAX Contributor Cosmos75's Avatar
    Joined
    May 2004
    Location
    Alabama, USA
    Posts
    118
    Location

    Solved!

    Sub delcols() 
    	Dim x, y As Range, z As Integer
    	x = Array(2, 4, 6, 8, 10)
    	Set y = Columns(x(0))
    	For z = 1 To UBound(x)
    		Set y = Union(y, Columns(x(z)))
    	Next z
    	y.Select
    	Selection.Delete Shift:=xlToLeft
    End Sub 

    Not my creation! See here.

  3. #3
    VBAX Contributor Aaron Blood's Avatar
    Joined
    Sep 2004
    Location
    Palm Beach, Florida, USA
    Posts
    130
    Location

    Lightbulb

    Thought you might also find this interesting...


    coltxt = "C4,C5,C7,C8,C10,C11,C13,C14"
    Range(Application.ConvertFormula(coltxt, xlR1C1, xlA1)).Delete


    Range syntax is also acceptable: "C4:C5, C7:C8"
    Last edited by Aussiebear; 04-20-2023 at 05:40 AM. Reason: Adjusted the code tags

  4. #4
    Dear Sir Aaron Blood,

    Just amazing , magical code...its very -very helpful to users.

    Regards,

    Chirag Raval

  5. #5
    Another way of attacking the problem.
    Sub Or_Like This()
        Dim Rng As Range
        On Error GoTo Canceled
            Set Rng = Application.InputBox(Prompt:="Select Columns With Mouse." & vbLf & "For Multiple Selections, hold "Ctrl" Button Down.", Title:="Select", Type:=8)
        Rng.Columns.Delete
    Canceled:
    End Sub

Posting Permissions

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