Excel VBA: how to format all as text in "text to columns" function

Js Smith

Board Regular
Joined
Jul 24, 2020
Messages
51
Office Version
  1. 2010
Platform
  1. Windows
Hi all, I'm chasing my tail on this one.
I can use
VBA Code:
Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, Comma:=True
and it works for a collection of CSV files.

If folks entered their data correctly on the CSV file there'd be 19 columns and
VBA Code:
FieldInfo :=Array(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 2), Array(5, 2), Array(6, 2), Array(7, 2), Array(8, 2), Array(9, 2), Array(10, 2), Array(11, 2), Array(12, 2), Array(13, 2), Array(14, 2), Array(15, 2), Array(16, 2), Array(17, 2), Array(18, 2), Array(19, 2))
would work.

Folks rarely enter their data (text or numbers) correctly so I need the FieldInfo part to be dynamic, in case we end up with more than 19 columns.

The goal is to have all the columns formatted as text, in case they put a number where text belongs. I can't install programs on this work computer to upload a worksheet but there's a picture to illustrate what I'm talking about.

How would you accomplish this?

Thanks!
 

Attachments

  • Capture.PNG
    Capture.PNG
    39.5 KB · Views: 17

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Not sure if there is a simpler, more direct way, but could this work for you?
Make the 'Const' line bigger than the number of columns that you would ever expect.

VBA Code:
Sub TTC()
  Dim ColAry()
  Dim i As Long
 
  Const MaxNum As Long = 40
 
  ReDim ColAry(1 To MaxNum)
  For i = 1 To MaxNum
    ColAry(i) = Array(i, 2)
  Next i
  Columns("A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, Comma:=True, FieldInfo:=ColAry
End Sub
 
Upvote 0
Solution
Not sure if there is a simpler, more direct way, but could this work for you.
Make the 'Const' line bigger than the number of columns that you would ever expect.

VBA Code:
Sub TTC()
  Dim ColAry()
  Dim i As Long
 
  Const MaxNum As Long = 40
 
  ReDim ColAry(1 To MaxNum)
  For i = 1 To MaxNum
    ColAry(i) = Array(i, 2)
  Next i
  Columns("A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, Comma:=True, FieldInfo:=ColAry
End Sub
Thanks Peter, that's what I was trying to do exactly. That Const MaxNum was my missing concept in my attempt.
Hope you have a wonderful day!
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,224,115
Messages
6,176,475
Members
452,728
Latest member
mihael546

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