Text file column to create variable with commas using VBA

jam1531

New Member
Joined
Jan 5, 2015
Messages
24
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.
 

jam1531

New Member
Joined
Jan 5, 2015
Messages
24
Sorry I left one important note out. The text file columns are separated by spaces, not tab delimited
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,434
Office Version
365
Platform
Windows
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
Joined
Jan 9, 2008
Messages
14,841
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
Joined
Jan 5, 2015
Messages
24
Apologies. I double checked and it actually is tab delimited.
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
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
 

Forum statistics

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

Some videos you may like

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...
Top