Text file column to create variable with commas using VBA

jam1531

New Member
Joined
Jan 5, 2015
Messages
29
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...
DogGrey01/02/19
CatGreen01/02/19
BirdPink01/02/19
SnakeGreen01/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.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Sorry I left one important note out. The text file columns are separated by spaces, not tab delimited
 
Upvote 0
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)?
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,240
Members
448,555
Latest member
RobertJones1986

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