Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / May 2008

Tip: Looking for answers? Try searching our database.

looping through comma delimited text

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DZ - 24 May 2008 04:45 GMT
Hi

I need to do something with a comma delimited block of text.

....like
text ,  text, text, etc

Can someone help me write a loop to loop  through each block of text between
commas?

Thanks alot for any help
Rick Rothstein (MVP - VB) - 24 May 2008 05:06 GMT
Consider this...

Dim X As Long
Dim BlockOfText As String
Dim Fields() As String
BlockOfText = "text1, text2, text3, etc."
'   The next statement assumes a comma followed
'    by a space, as shown, is the delimiting the text
Fields = Split(BlockOfText, ", ")
'    The zero-based Fields array now holds
'    each text sub-string in its elements...
For X = 0 To UBound(Fields)
  Debug.Print Fields(X)
Next

Rick

> Hi
>
[quoted text clipped - 8 lines]
>
> Thanks alot for any help
Joel - 24 May 2008 05:08 GMT
Here is some code you might use

Sub GetCSVData()

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Const Delimiter = ","
Set fsread = CreateObject("Scripting.FileSystemObject")

'default folder
Folder = "C:\temp\test"
ChDir (Folder)

FName = Application.GetOpenFilename("CSV (*.csv),*.csv")

RowCount = LastRow + 1
If FName <> "" Then
     'open files
     Set fread = fsread.GetFile(FName)
     Set tsread = fread.OpenAsTextStream(ForReading, TristateUseDefault)

     Do While tsread.atendofstream = False
     
        InputLine = tsread.ReadLine

        'extract comma seperated data
        ColumnCount = 1
        Do While InputLine <> ""
           DelimiterPosition = InStr(InputLine, Delimiter)
           If DelimiterPosition > 0 Then
              Data = Trim(Left(InputLine, DelimiterPosition - 1))
              InputLine = Mid(InputLine, DelimiterPosition + 1)
           Else
              Data = Trim(InputLine)
              InputLine = ""
           End If
           
           Cells(RowCount, ColumnCount) = Data
           ColumnCount = ColumnCount + 1
        Loop
        RowCount = RowCount + 1
     Loop
       
     tsread.Close
End If
End Sub

> Hi
>
[quoted text clipped - 7 lines]
>
> Thanks alot for any help
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.