P
potter.justin
Hi,
I am trying to come up with a way to import multiple text files into
excel. What I want to do is import each text file to a seperate
worksheet, I would like to be able to run the macro once a week and
pull in any new (or all) the text files into excel. If it is easier
to just pull the whole folder I want the existing sheets to be
overwritten. I have somewhat limited experience with VBA, but after
browsing the forums this is what i have come up with:
Sub GetFiles()
Dim sPath As String, sName As String
Dim i As Long, qt As QueryTable
Dim sSheet As String
sPath = "C:\Documents and Settings\jpotter\Desktop\CSV\"
sName = Dir(sPath & "*.txt")
i = 0
Do While sName <> ""
i = i + 1
Cells(1, i).Value = sName
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & sPath & sName, Destination:=Cells(1, 1))
.Name = Left(sName, Len(sName) - 4)
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
NewSheetName = "Sheet" + Str(i)
For j = 1 To Sheets.Count
If TypeName(Sheets(j)) = "Worksheet" Then
MyWorkSheetName = Sheets(j).Name
Else
End If
If MyWorkSheetName = NewSheetName Then
j = j + 1
'Next
Else
NewSheetName = "Sheet" + Str(i)
ActiveWorkbook.Worksheets.Add.Name = NewSheetName
Worksheets(NewSheetName).Select
End If
j = j + 1
Next
sName = Dir()
For Each qt In ActiveSheet.QueryTables
qt.Delete
Next
Loop
'ActiveDocument.Save
End Sub
My problem that I am running into is that excel does not like the way
I am checking to see if the sheet is existing before creating a new
one. Any help would be greatly appreaciated.
Thanks,
Justin
I am trying to come up with a way to import multiple text files into
excel. What I want to do is import each text file to a seperate
worksheet, I would like to be able to run the macro once a week and
pull in any new (or all) the text files into excel. If it is easier
to just pull the whole folder I want the existing sheets to be
overwritten. I have somewhat limited experience with VBA, but after
browsing the forums this is what i have come up with:
Sub GetFiles()
Dim sPath As String, sName As String
Dim i As Long, qt As QueryTable
Dim sSheet As String
sPath = "C:\Documents and Settings\jpotter\Desktop\CSV\"
sName = Dir(sPath & "*.txt")
i = 0
Do While sName <> ""
i = i + 1
Cells(1, i).Value = sName
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & sPath & sName, Destination:=Cells(1, 1))
.Name = Left(sName, Len(sName) - 4)
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
NewSheetName = "Sheet" + Str(i)
For j = 1 To Sheets.Count
If TypeName(Sheets(j)) = "Worksheet" Then
MyWorkSheetName = Sheets(j).Name
Else
End If
If MyWorkSheetName = NewSheetName Then
j = j + 1
'Next
Else
NewSheetName = "Sheet" + Str(i)
ActiveWorkbook.Worksheets.Add.Name = NewSheetName
Worksheets(NewSheetName).Select
End If
j = j + 1
Next
sName = Dir()
For Each qt In ActiveSheet.QueryTables
qt.Delete
Next
Loop
'ActiveDocument.Save
End Sub
My problem that I am running into is that excel does not like the way
I am checking to see if the sheet is existing before creating a new
one. Any help would be greatly appreaciated.
Thanks,
Justin