|
-
Apr 9th, 2009, 10:17 PM
#1
Thread Starter
Member
open textfile in local drive and save it as a delimited file
Code:
Dim strLoad As String
Dim bk As Workbook
Dim sPath As String
sPath = "C:\data\"
strLoad = Dir("C:\data\*.txt")
Do While strLoad > vbNullString
'code goes here to import into excel
Workbooks.OpenText Filename:= _
strLoad, Origin:=xlWindows, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, Comma:=False, _
Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3 _
, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1))
Set bk = ActiveWorkbook
bk.SaveAs sPath & Left(bk.Name, Len(bk.Name) - 4) _
& ".xls", FileFormat:=xlWorkbookNormal
bk.Close Savechanges:=False
strLoad = Dir
Loop
Hi I try to use the above code to open all the textfiles in c:\data. I need the script to open the textfiles in excel as save it as a delimited file. If i do it manually, i will have to perform the following steps:
In excel click open-->choose textfile-->delimited-->check space and tab-->next-->finish-->save the file in its original location as a delimited file-->close the file.
I encounter runtime error 1004 when i try to run the code. I am using excel 2003 and the visual basic editor in excel itself. Hope you can help me
Rgds
-
Apr 10th, 2009, 01:13 AM
#2
Re: open textfile in local drive and save it as a delimited file
strload does not contain the path, only the file name, you need
spath & strload for opening
which line gives error?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 10th, 2009, 03:20 AM
#3
Thread Starter
Member
Re: open textfile in local drive and save it as a delimited file
Hi, runtime error is encountered when i reaches the line of
Workbooks.OpenText Filename:= _
strLoad, Origin:=xlWindows, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, Comma:=False, _
Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3 _
, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1))
-
Apr 10th, 2009, 04:02 AM
#4
Re: open textfile in local drive and save it as a delimited file
check it is finding the file as you are only passing the filename with no path, see my comment above
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|