VBA - Select Multiple Columns using Column Number

Cosmos75

Active Member
Joined
Feb 28, 2002
Messages
359
Is there a way to select multiple columns in VBA using the column number?

I have this
Code:
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.
 
Sorry to post on such an old thread, but I thought this to be quite useful in case you are looking at deleting continuous columns:

Code:
ActiveSheet.columns(8).Resize(ActiveSheet.Rows.Count, ActiveSheet.columns.Count - 8).Delete
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Like everyone else who posted late, I feel compelled to. It's actually quite simple to do if you use the "EntireColumn" function on a range of cells:

Code:
Range(Cells(2, 14), Cells(2, 15)).EntireColumn.Select

This will select columns 14 and 15 (i.e. columns N and O). Good for contiguous columns, and allows you to replace the "14" and "15" with variables if needed.
 
Upvote 0
Perhaps something like this:

Code:
Sub delcols()
Dim x, y As Range, z As Integer
x = Array(14, 13, 11, 10, 8, 7, 5, 4)
Set y = Columns(x(0))
For z = 1 To UBound(x)
Set y = Union(y, Columns(x(z)))
Next z
y.Delete Shift:=xlToLeft
End Sub

Dear Sir Scott Huish,

Amazing Code, Really Great...But can it be modify that it select only some ranges' (either range set as object or named range's columns) instead of entire column of sheet?

for example if my stetted range is "A1 to W500", then how this code select non contiguous columns of this range only and not entire columns of sheet?


Hope there are some solution found.


Regards,

Chirag Raval
 
Last edited:
Upvote 0
Dear Sir Scott Huish,

Amazing Code, Really Great...But can it be modify that it select only some ranges' (either range set as object or named range's columns) instead of entire column of sheet?

for example if my stetted range is "A1 to W500", then how this code select non contiguous columns of this range only and not entire columns of sheet?


Hope there are some solution found.


Regards,

Chirag Raval
Is this the sort of thing you mean? This will delete the first 500 rows of columns A, E:G and T and move everything left.
Test in a copy of your workbook.

If it isn't what you meant, please try to explain more clearly and/or give examples.

Code:
Sub DeletePartialColumns()
  Intersect(Rows("1:500"), Range("A:A, E:G, T:T")).Delete Shift:=xlToLeft
End Sub
 
Upvote 0
Is this the sort of thing you mean? This will delete the first 500 rows of columns A, E:G and T and move everything left.
Test in a copy of your workbook.

If it isn't what you meant, please try to explain more clearly and/or give examples.

Code:
Sub DeletePartialColumns()
  Intersect(Rows("1:500"), Range("A:A, E:G, T:T")).Delete Shift:=xlToLeft
End Sub

Dear Sir @Peter_SSs

Thank you very much for your kind help..

Yes another smart way you teach us,.
your code just do as I wish but...can it work on dynamic row instead of fixed row reference ?
I modify your code to just select/ or process something like below I apply "Bold" to desired columns.

Code:
Sub ProcessPartialColumns()
  Intersect(Rows("1:500"), Range("A:A, E:G, T:T")).Font.Bold = True
End Sub

Can I use your valuable approach as below as use of LastRow Variable instead of fixed & hard coded rows numbers reference?

myrng.Intersect(Rows("1:LastRow"), Range("A:A, E:G, T:T")).Font.Bold = True



below is screen shot of my more precise requirement
[IMG]http://i64.tinypic.com/339irlu.jpg[/IMG]


Hope there are some solution found.

Regards,

Chirag Raval
 
Last edited:
Upvote 0
Dear Sir @scott huish,

Amazing Code, Really Great...But can it be modify that it select only some ranges' (either range set as object or named range's columns) instead of entire column of sheet?

for example if my stetted range is "A1 to W500", then how this code select non contiguous columns of this range only and not entire columns of sheet?


Hope there are some solution found.


Regards,

Chirag Raval

Dear All Respected experts & Users,

Yes ...I modify code as I require & apply successful to my dynamic range stored in object variable.

here is successful modification of Mr @scott huise 's code.

Code:
Sub SelDeleteProcessNonContColmnsok()
Dim x, y As Range, z As Integer
Dim lastrow As Long
Dim myrng As Range
With ActiveSheet
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set myrng = .Range("A1:W" & lastrow)
With myrng
x = Array(14, 13, 11, 10, 8, 7, 5, 4)
Set y = myrng.Columns(x(0))
For z = 1 To UBound(x)
Set y = Union(y, myrng.Columns(x(z)))
Next z
'y.Delete Shift:=xlToLeft
y.Select
End With
End With

set myrng=nothing

End Sub

I just select this range as test ,but uses can do anything on columns of this dynamic range .

Hope This will be helpful.

Thank you very much for all expert's & user's for great help.

Regards,

Chirag Raval
 
Upvote 0
your code just do as I wish but...can it work on dynamic row instead of fixed row reference ?
I modify your code to just select/ or process something like below I apply "Bold" to desired columns.
Sure, here's an example, assuming column A can be used to determine the last row
Code:
Sub WorkWithPartialColumns()
  With Intersect(Rows("1:" & Range("A" & Rows.Count).End(xlUp).Row), Range("A:A, E:G, T:T"))
    .Font.Bold = True
    .Interior.Color = vbBlue
  End With
End Sub

or if you want to use a LastRow variable

Code:
  With Intersect(Rows("1:" & LastRow), Range("A:A, E:G, T:T"))
    .Font.Bold = True
    .Interior.Color = vbBlue
  End With
 
Upvote 0
Sure, here's an example, assuming column A can be used to determine the last row
Code:
Sub WorkWithPartialColumns()
  With Intersect(Rows("1:" & Range("A" & Rows.Count).End(xlUp).Row), Range("A:A, E:G, T:T"))
    .Font.Bold = True
    .Interior.Color = vbBlue
  End With
End Sub

or if you want to use a LastRow variable

Code:
  With Intersect(Rows("1:" & LastRow), Range("A:A, E:G, T:T"))
    .Font.Bold = True
    .Interior.Color = vbBlue
  End With


Dear Sir @Peter_SSs,

Amazing, Super , Magical

Your guideline just amazingly work on dynamic range also..

sorry to ask , if you don't mine , same process can be do with column no instead of columns name?
means use Range(1,7,9:11,17,22) instead of range("A:A, E:G, T:T"))?

Hope there are some solutions.

Again thank you for give your valuable time & effort.

Regards,

Chirag Raval
 
Upvote 0
Not so easy with multiple disjoint columns using numbers. Depends a bit exactly what you have and what you are trying to do. Options would be to loop through the columns individually, or to use union like this
Code:
With Intersect(Rows("1:" & LastRow), Union(Columns(1), Columns(7), Columns(9).Resize(, 3), Columns(17), Columns(22)))
 
Upvote 0
Not so easy with multiple disjoint columns using numbers. Depends a bit exactly what you have and what you are trying to do. Options would be to loop through the columns individually, or to use union like this
Code:
With Intersect(Rows("1:" & LastRow), Union(Columns(1), Columns(7), Columns(9).Resize(, 3), Columns(17), Columns(22)))

Dear Sir @Peter_SSs

Just Great , Amazing,, Again Super Shot,
Though it is pure one liner code, & if we don't mine to just type little long as ,
Code:
With Intersect(Rows("1:" & LastRow), Union(Columns(1), Columns(7), Columns(9).Resize(, 3), Columns(17), Columns(22)))

in short we can use "column number" in this approach.

And yes, if we do not hesitate for "Options would be to loop through the columns individually"
that definitely will also be very useful approach towards process of columns of range , with column numbers.

hope your help sir for Loop Approach.

Regards,

Chirag Raval
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,716
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top