LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Importing Multiple Text File in Excel

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

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing multiple Text files into Excel [email protected] Excel Programming 2 April 9th 06 01:35 PM
Importing multiple Text files into Excel [email protected] Excel Programming 0 April 8th 06 05:21 PM
Importing multiple Text files into Excel [email protected] Excel Programming 0 April 8th 06 05:21 PM
Importing Text File into Excel Martin Excel Discussion (Misc queries) 1 April 2nd 06 02:48 PM
Importing text file to excel dany04 Excel Discussion (Misc queries) 1 November 9th 05 01:13 AM


All times are GMT +1. The time now is 08:57 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"