# Text file column to create variable with commas using VBA

#### jam1531

##### New Member
Hello,

I am looking for some code that will do the following. When the macro is run it will pull only the first column from a text file (path is C:\Users\jmckibb\Desktop\My Stuff\text.txt) and create a variable (if that is the right terminology) that is the first column with a comma behind every row except the last.

For example:

The test file is like so...
 Dog Grey 01/02/19 Cat Green 01/02/19 Bird Pink 01/02/19 Snake Green 01/02/19

<tbody>
</tbody>

When the macro is run I need it to bring back the following and name it "List"

List = "Dog, Cat, Bird, Snake"

It is important that commas are behind each one except the last as I will be using this List to go into SQL and pull further data.

Any help appreciated.

#### jam1531

##### New Member
Sorry I left one important note out. The text file columns are separated by spaces, not tab delimited

#### Joe4

##### MrExcel MVP, Junior Admin
The text file columns are separated by spaces, not tab delimited
So, exactly how many spaces can the first field contain (or to put it another way, where does the second field begin)?

#### MickG

##### MrExcel MVP
Try this:-
NB:- Alter Path to suit !!!
Code:
[COLOR="Navy"]Sub[/COLOR] MG02Jan01
[COLOR="Navy"]Dim[/COLOR] ray, Rw [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] List [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
Open "C:\Users\USER1\Desktop\test.txt" [COLOR="Navy"]For[/COLOR] Input [COLOR="Navy"]As[/COLOR] [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]
ray = Split(Input(LOF(1), [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] ), vbLf)
[COLOR="Navy"]For[/COLOR] Rw = 0 To UBound(ray)
List = List & IIf(List = "", Split(ray(Rw), Chr(32))(0), ", " & Split(ray(Rw), Chr(32))(0))
[COLOR="Navy"]Next[/COLOR] Rw
Range("A1") = List
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick

#### jam1531

##### New Member
Apologies. I double checked and it actually is tab delimited.

#### MickG

##### MrExcel MVP
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG03Jan52
[COLOR="Navy"]Dim[/COLOR] ray, Rw [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] List [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
Open "C:\Users\USER1\Desktop\test3.txt" [COLOR="Navy"]For[/COLOR] Input [COLOR="Navy"]As[/COLOR] [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]
ray = Split(Input(LOF(1), [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] ), vbLf)
[COLOR="Navy"]For[/COLOR] Rw = 0 To UBound(ray) - 1
List = List & IIf(List = "", Split(ray(Rw), Chr(9))(0), ", " & Split(ray(Rw), Chr(9))(0))
[COLOR="Navy"]Next[/COLOR] Rw
Range("A1") = List
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick

#### jam1531

##### New Member
Works like a charm. Thanks!

You're welcome

1,081,706
Messages
5,360,769
Members
400,595
Latest member
T_Dubs

### This Week's Hot Topics

• VBA (Userform)
Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
• List box that changes fill color
Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
• Remove duplicates and retain one. Cross-linked cases
Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
• VBA Copy and Paste With Duplicates
Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
• Macro
is it possible for a macro to run if the active cell value is different to the value above it
• IF DATE and TIME
I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...