adding a string of data to an array

R

R Tanner

Hi,

I am trying to cycle through a series of files in a folder and if the
filename does not meet my criteria, then I want to add the filename to
my array. This is my loop. How do I add the filename to my array?

Sub ParseTextFiles()

Dim MyFile As String
Dim FSO As FileSystemObject
Dim MyFolder As Object
Dim ObjFile As Object
Dim colFiles As Object
Dim MyParsedMessage() As String
Dim MyParsedDate As String



MyFile = "Z:\Drop Box\robin.tanner\FortexRejects"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set MyFolder = FSO.GetFolder(MyFile)
Set colFiles = MyFolder.Files

For Each ObjFile In colFiles
MyParsedDate =
Application.WorksheetFunction.Substitute(Left(ObjFile.Name, 10), ".",
"/")
If Not IsDate(MyParsedDate) Then
MyParsedMessage(i) = ObjFile.Name
End If
Next

MsgBox MyParsedMessage(1:i)


End Sub
 
J

Jim Thomlinson

You need a dynamic array. This is untested but it sould be close. I
personally would have used a collection of file objects but to each his own...

Sub ParseTextFiles()

Dim MyFile As String
Dim FSO As FileSystemObject
Dim MyFolder As Object
Dim ObjFile As Object
Dim colFiles As Object
Dim MyParsedMessage() As String
Dim MyParsedDate As String
Dim lng as long

MyFile = "Z:\Drop Box\robin.tanner\FortexRejects"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set MyFolder = FSO.GetFolder(MyFile)
Set colFiles = MyFolder.Files

For Each ObjFile In colFiles
MyParsedDate =
Application.WorksheetFunction.Substitute(Left(ObjFile.Name, 10), ".",
"/")
If Not IsDate(MyParsedDate) Then
Redim Preserve MyParsedMessage(lng)
MyParsedMessage(lng) = ObjFile.Name
lng = lng + 1
End If
Next

for lng = lbound(MyParsedMessage) to ubound(MyParsedMessage)
MsgBox MyParsedMessage(1:i)
next lng
End Sub
 
R

R Tanner

You need a dynamic array. This is untested but it sould be close. I
personally would have used a collection of file objects but to each his own...

Sub ParseTextFiles()

Dim MyFile As String
Dim FSO As FileSystemObject
Dim MyFolder As Object
Dim ObjFile As Object
Dim colFiles As Object
Dim MyParsedMessage() As String
Dim MyParsedDate As String
Dim lng as long

MyFile = "Z:\Drop Box\robin.tanner\FortexRejects"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set MyFolder = FSO.GetFolder(MyFile)
Set colFiles = MyFolder.Files

For Each ObjFile In colFiles
    MyParsedDate =
Application.WorksheetFunction.Substitute(Left(ObjFile.Name, 10), ".",
"/")
    If Not IsDate(MyParsedDate) Then
        Redim Preserve MyParsedMessage(lng)
        MyParsedMessage(lng) = ObjFile.Name
        lng = lng + 1
    End If
Next

for lng = lbound(MyParsedMessage) to ubound(MyParsedMessage)
  MsgBox MyParsedMessage(1:i)
next lng
End Sub

--
HTH...

Jim Thomlinson













- Show quoted text -

oh I am more than open to suggestions Jim. I am just using what I
know. Please, tell me why a collection of file objects would be
better in your opinion...

Thank you for the fix by the way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top