VBA Select all columns but one

LNG2013

Active Member
Joined
May 23, 2011
Messages
466
In VBA how can I select all columns but one?
I need to select all of my columns except for Time_Stamp, and have all of the the select columns set to to text.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
In VBA how can I select all columns but one?
I need to select all of my columns except for Time_Stamp, and have all of the the select columns set to to text.


Example:

Assume only Columns A-Z exist and we want to select all except X,
Code:
Range("A:W,Y:Z").Select


Is this what you are looking for?

What do you mean "set to to text"?

Set the format for the selected columns to TEXT?

Code:
Range("A:W,Y:Z").Select
Selection.NumberFormat = "@"

The above code formats Columns A-W, Y, Z to TEXT (skips column X)
 
Last edited:
Upvote 0
Sorry my columns change order, meaning what is A one day might be C the next, so I was looking to select all omitting the Time_Stamp column.... by "set to text," I meant to format all of those columns to text.

Thanks for your help!
 
Upvote 0
Maybe like this

Code:
Sub test()
Dim Except As Long, LC As Long, j
Except = Rows(1).Find(what:="Time_Stamp", LookIn:=xlValues, lookat:=xlWhole).Column
LC = Cells(1, Columns.Count).End(xlToLeft).Column
For j = 1 To LC
    If j <> Except Then Columns(j).NumberFormat = "@"
Next j
End Sub
 
Upvote 0
Sorry my columns change order, meaning what is A one day might be C the next, so I was looking to select all omitting the Time_Stamp column.... by "set to text," I meant to format all of those columns to text.

Thanks for your help!
Are you saying that Cell 1 in each column contains a heading?
 
Upvote 0
Maybe like this

Code:
Sub test()
Dim Except As Long, LC As Long, j
Except = Rows(1).Find(what:="Time_Stamp", LookIn:=xlValues, lookat:=xlWhole).Column
LC = Cells(1, Columns.Count).End(xlToLeft).Column
For j = 1 To LC
    If j <> Except Then Columns(j).NumberFormat = "@"
Next j
End Sub
Peter you are too quick!! I was stumbling my way through writing something much less concise.

This does the trick.
 
Upvote 0
VoG!

Bam!

You Sir are fantastic! :biggrin:

That works, I worked this into my code after it imports the csv to keep my columns from getting auto formatted!
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,225
Members
452,896
Latest member
IGT

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