Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA-How to open several delmited files & SaveAs Excel files

I have several delimited files (.xl) in the same directory and would
like to open all of them. I would like to save each file in the same
name except as an Excel file. I have macros that open all files in a
directory, save all open workbooks and open delimited files given a
name (in an array in the macro). Any suggestions would be
appreciated!

Below is what I currently have to open the files, notice how each file
name must be specified in the array. I basically want to get rid of
this necessity.

Sub Open1()
Dim varr As Variant
Dim wkbk1 As Workbook
Dim wkbk As Workbook
Dim i As Long
Dim sh1 As Workbook
Dim sName As String

varr = Array("12 15 03 01 NIST610.xl", _
"12 15 03 02 NIST610.xl", _
"12 15 03 03 NIST610.xl", _
"12 15 03 04 MACS1.xl")

Set wkbk = ActiveWorkbook
For i = LBound(varr) To UBound(varr)
Workbooks.OpenText Filename:=tName, _
Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited,
TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True,
Semicolon:=False, _
Comma:=True, Space:=False, 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), Array(10, 1), Array(11, 1), Array(12, 1), Array(13,
1), Array(14, 1), Array(15 _
, 1), Array(16, 1), Array(17, 1), Array(18, 1), Array(19, 1),
Array(20, 1), Array(21, 1), _
Array(22, 1), Array(23, 1), Array(24, 1))

Set wkbk1 = ActiveWorkbook

Next
End Sub

I'll also include my code to open all files within a directory. Can I
modify this to open all delimited files and automatically go through
the delimited-to-excel file wizard?

Sub OpenAll()
Dim sName As String
sName = Dir("D:\Key West\New Data\*.xls")
Do While sName < ""
Workbooks.Open Filename:=sName
sName = Dir()
Loop
End Sub

Thank you!

Ryan


---
Message posted from http://www.ExcelForum.com/

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Excel VBA-How to open several delmited files & SaveAs Excel files


You can combine them to achieve this.

Sub OpenAll()
Dim varr As Variant
Dim wkbk1 As Workbook
Dim wkbk As Workbook
Dim i As Long
Dim sh1 As Workbook
Dim sName As String
Dim sPath as string
Dim ub as Long
Redim Varr(1 to 1)
ub = 1
sPath = "D:\Key West\New Data\"
sName = Dir(spath & "*.xl")
Do While sName < ""
Redim Preserve varr(1 to ub)
varr(ub) = sName
ub = ub + 1
sName = Dir()
Loop

Set wkbk = ActiveWorkbook
For i = LBound(varr) To UBound(varr)
tName = sPath & varr(i)
Workbooks.OpenText Filename:=tName, _
Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited,
TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True,
Semicolon:=False, _
Comma:=True, Space:=False, 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), Array(10, 1), Array(11, 1), Array(12, 1), Array(13,
1), Array(14, 1), Array(15 _
, 1), Array(16, 1), Array(17, 1), Array(18, 1), Array(19, 1),
Array(20, 1), Array(21, 1), _
Array(22, 1), Array(23, 1), Array(24, 1))

Set wkbk1 = ActiveWorkbook

' any processing

wkbk1.SaveAs Filename = left(wkbk1.name, len(wkbk1.name)-4 & ".xls", _
Fileformat:=xlWorkbookNormal
wkbk1.Close SaveChanges:=False

Next
End Sub

--
Regards,
Tom Ogilvy



"waveracerr " wrote in message
...
I have several delimited files (.xl) in the same directory and would
like to open all of them. I would like to save each file in the same
name except as an Excel file. I have macros that open all files in a
directory, save all open workbooks and open delimited files given a
name (in an array in the macro). Any suggestions would be
appreciated!

Below is what I currently have to open the files, notice how each file
name must be specified in the array. I basically want to get rid of
this necessity.

Sub Open1()
Dim varr As Variant
Dim wkbk1 As Workbook
Dim wkbk As Workbook
Dim i As Long
Dim sh1 As Workbook
Dim sName As String

varr = Array("12 15 03 01 NIST610.xl", _
"12 15 03 02 NIST610.xl", _
"12 15 03 03 NIST610.xl", _
"12 15 03 04 MACS1.xl")

Set wkbk = ActiveWorkbook
For i = LBound(varr) To UBound(varr)
Workbooks.OpenText Filename:=tName, _
Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited,
TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True,
Semicolon:=False, _
Comma:=True, Space:=False, 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), Array(10, 1), Array(11, 1), Array(12, 1), Array(13,
1), Array(14, 1), Array(15 _
, 1), Array(16, 1), Array(17, 1), Array(18, 1), Array(19, 1),
Array(20, 1), Array(21, 1), _
Array(22, 1), Array(23, 1), Array(24, 1))

Set wkbk1 = ActiveWorkbook

Next
End Sub

I'll also include my code to open all files within a directory. Can I
modify this to open all delimited files and automatically go through
the delimited-to-excel file wizard?

Sub OpenAll()
Dim sName As String
sName = Dir("D:\Key West\New Data\*.xls")
Do While sName < ""
Workbooks.Open Filename:=sName
sName = Dir()
Loop
End Sub

Thank you!

Ryan


---
Message posted from http://www.ExcelForum.com/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA-How to open several delmited files & SaveAs Excel files

Thanks for the help, I still cannot get the macro to save the workbook.
Excel gives me an error, "Run-time error '13': Type mismatch".
included a missing ")" after the number "4" in the code and then fin
Excel telling me the file "False.xls" already exists and do I wish t
save over it... I click "No" and then find the following code i
highlighted in debug mode:

wkbk1.SaveAs Filename = Left(wkbk1.Name, Len(wkbk1.Name) - 4) & ".xls"
_
FileFormat:=xlWorkbookNormal

I wonder if the code under the above code has anything to do with thi
problem?

wkbk1.Close SaveChanges:=False

Thanks for any help!

Rya

--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Excel VBA-How to open several delmited files & SaveAs Excel files

Hard to tell what you did. For example, in your original code

Set wkbk = ActiveWorkbook
For i = LBound(varr) To UBound(varr)
Workbooks.OpenText Filename:=tName, _

you open a workbook tName, but you never set that variable so your array of
names it totally unused. Yet you say the code is working.

Yes, I did omit a closing paren:

wkbk1.SaveAs Filename = left(wkbk1.name, len(wkbk1.name)-4 & ".xls", _

should be

wkbk1.SaveAs Filename = left(wkbk1.name, len(wkbk1.name)-4) & ".xls", _

No, there is nothing wrong with the statement you cite - since it hasn't
been executed yet when your error occurs, that would be even more reason not
to look there.

for some reason, it is opening a workbook named False.xl I would assume.
Probably the product of some previously flawed code being run.

If you want to delete existing workbooks with duplicate names, you can use
the kill command

Or you can suppress the message with DiplayAlerts.

--
Regards,
Tom Ogilvy



"waveracerr " wrote in message
...
Thanks for the help, I still cannot get the macro to save the workbook.
Excel gives me an error, "Run-time error '13': Type mismatch". I
included a missing ")" after the number "4" in the code and then find
Excel telling me the file "False.xls" already exists and do I wish to
save over it... I click "No" and then find the following code is
highlighted in debug mode:

wkbk1.SaveAs Filename = Left(wkbk1.Name, Len(wkbk1.Name) - 4) & ".xls",
_
FileFormat:=xlWorkbookNormal

I wonder if the code under the above code has anything to do with this
problem?

wkbk1.Close SaveChanges:=False

Thanks for any help!

Ryan


---
Message posted from http://www.ExcelForum.com/



Reply
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
emailing files from excel, the files will not go until I open up . joe New Users to Excel 2 September 18th 09 02:12 PM
how do I toggle between 2 open excel files and leave both open Big D in Brighton Excel Discussion (Misc queries) 1 November 6th 08 04:28 PM
How to change default Open/Files of Type to "Microsoft Excel Files Tammy Excel Discussion (Misc queries) 2 January 14th 08 11:06 PM
I can't open any excel files Laura Gravina Excel Discussion (Misc queries) 0 October 12th 06 08:26 PM
Excel files won't open Mason C Excel Discussion (Misc queries) 1 February 28th 06 09:26 AM


All times are GMT +1. The time now is 05:00 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"