PC Review


Reply
Thread Tools Rate Thread

Convert multiple csv files into one excel file

 
 
Chris Lewis
Guest
Posts: n/a
 
      4th Oct 2006
Can anyone help me. I want to convert a number of csv files (all in the
same folder) into an excel file. I would like each csv to be a worksheet in
the excel file with the sheet name the same as the csv file name but without
the .csv file extension.

I guess it would be easy if I was prompted for which folder I want to use as
source as I have to do this over and over again!!

--
Chris Lewis


 
Reply With Quote
 
 
 
 
PY & Associates
Guest
Posts: n/a
 
      4th Oct 2006
We believe the macro recorder can do this job.

Add a new worksheet;
get new filename;
name the worksheet with filename;
import the csv file;

continue with next file

Regards
"Chris Lewis" <(E-Mail Removed)> wrote in message
news:45237d7a$(E-Mail Removed)...
> Can anyone help me. I want to convert a number of csv files (all in the
> same folder) into an excel file. I would like each csv to be a worksheet

in
> the excel file with the sheet name the same as the csv file name but

without
> the .csv file extension.
>
> I guess it would be easy if I was prompted for which folder I want to use

as
> source as I have to do this over and over again!!
>
> --
> Chris Lewis
>
>



 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      4th Oct 2006
Try this one Chris

Use this macro then if the csv files are in C:\Data
More info here
http://www.rondebruin.nl/copy3.htm


Sub Example()
Dim MyPath As String
Dim FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long
Dim Fnum As Long
Dim mybook As Workbook
Dim basebook As Workbook


'Fill in the path\folder where the files are
'on your machine
MyPath = "c:\Data"


'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If


'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.csv")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If


On Error GoTo CleanUp


Application.ScreenUpdating = False
Set basebook = ThisWorkbook


'Fill the array(myFiles)with the list of csv files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop


'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
mybook.Worksheets(1).Copy after:= _
basebook.Sheets(basebook.Sheets.Count)


On Error Resume Next
ActiveSheet.Name = mybook.Name
On Error GoTo 0


' You can use this if you want to copy only the values
' With ActiveSheet.UsedRange
' .Value = .Value
' End With


mybook.Close savechanges:=False
Next Fnum
End If
CleanUp:
Application.ScreenUpdating = True
End Sub



--
Regards Ron de Bruin
http://www.rondebruin.nl



"Chris Lewis" <(E-Mail Removed)> wrote in message news:45237d7a$(E-Mail Removed)...
> Can anyone help me. I want to convert a number of csv files (all in the same folder) into an excel file. I would like each csv
> to be a worksheet in the excel file with the sheet name the same as the csv file name but without the .csv file extension.
>
> I guess it would be easy if I was prompted for which folder I want to use as source as I have to do this over and over again!!
>
> --
> Chris Lewis
>



 
Reply With Quote
 
Mustang
Guest
Posts: n/a
 
      4th Oct 2006
Try windows ms-dos

in folder where the file are: example c:\ myfolder
write, copy *.csv to c:\ all.csv
and next.

In excel import c:\all.csv

Best regard

Andrew
Ron de Bruin ha escrito:

> Try this one Chris
>
> Use this macro then if the csv files are in C:\Data
> More info here
> http://www.rondebruin.nl/copy3.htm
>
>
> Sub Example()
> Dim MyPath As String
> Dim FilesInPath As String
> Dim MyFiles() As String
> Dim SourceRcount As Long
> Dim Fnum As Long
> Dim mybook As Workbook
> Dim basebook As Workbook
>
>
> 'Fill in the path\folder where the files are
> 'on your machine
> MyPath = "c:\Data"
>
>
> 'Add a slash at the end if the user forget it
> If Right(MyPath, 1) <> "\" Then
> MyPath = MyPath & "\"
> End If
>
>
> 'If there are no Excel files in the folder exit the sub
> FilesInPath = Dir(MyPath & "*.csv")
> If FilesInPath = "" Then
> MsgBox "No files found"
> Exit Sub
> End If
>
>
> On Error GoTo CleanUp
>
>
> Application.ScreenUpdating = False
> Set basebook = ThisWorkbook
>
>
> 'Fill the array(myFiles)with the list of csv files in the folder
> Fnum = 0
> Do While FilesInPath <> ""
> Fnum = Fnum + 1
> ReDim Preserve MyFiles(1 To Fnum)
> MyFiles(Fnum) = FilesInPath
> FilesInPath = Dir()
> Loop
>
>
> 'Loop through all files in the array(myFiles)
> If Fnum > 0 Then
> For Fnum = LBound(MyFiles) To UBound(MyFiles)
> Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
> mybook.Worksheets(1).Copy after:= _
> basebook.Sheets(basebook.Sheets.Count)
>
>
> On Error Resume Next
> ActiveSheet.Name = mybook.Name
> On Error GoTo 0
>
>
> ' You can use this if you want to copy only the values
> ' With ActiveSheet.UsedRange
> ' .Value = .Value
> ' End With
>
>
> mybook.Close savechanges:=False
> Next Fnum
> End If
> CleanUp:
> Application.ScreenUpdating = True
> End Sub
>
>
>
> --
> Regards Ron de Bruin
> http://www.rondebruin.nl
>
>
>
> "Chris Lewis" <(E-Mail Removed)> wrote in message news:45237d7a$(E-Mail Removed)...
> > Can anyone help me. I want to convert a number of csv files (all in the same folder) into an excel file. I would like each csv
> > to be a worksheet in the excel file with the sheet name the same as the csv file name but without the .csv file extension.
> >
> > I guess it would be easy if I was prompted for which folder I want to use as source as I have to do this over and over again!!
> >
> > --
> > Chris Lewis
> >


 
Reply With Quote
 
Mustang
Guest
Posts: n/a
 
      4th Oct 2006
Try windows ms-dos

in folder where the file are: example c:\ myfolder
write, copy *.csv to c:\ all.csv
and next.

In excel import c:\all.csv

Best regard

Andrew
Ron de Bruin ha escrito:

> Try this one Chris
>
> Use this macro then if the csv files are in C:\Data
> More info here
> http://www.rondebruin.nl/copy3.htm
>
>
> Sub Example()
> Dim MyPath As String
> Dim FilesInPath As String
> Dim MyFiles() As String
> Dim SourceRcount As Long
> Dim Fnum As Long
> Dim mybook As Workbook
> Dim basebook As Workbook
>
>
> 'Fill in the path\folder where the files are
> 'on your machine
> MyPath = "c:\Data"
>
>
> 'Add a slash at the end if the user forget it
> If Right(MyPath, 1) <> "\" Then
> MyPath = MyPath & "\"
> End If
>
>
> 'If there are no Excel files in the folder exit the sub
> FilesInPath = Dir(MyPath & "*.csv")
> If FilesInPath = "" Then
> MsgBox "No files found"
> Exit Sub
> End If
>
>
> On Error GoTo CleanUp
>
>
> Application.ScreenUpdating = False
> Set basebook = ThisWorkbook
>
>
> 'Fill the array(myFiles)with the list of csv files in the folder
> Fnum = 0
> Do While FilesInPath <> ""
> Fnum = Fnum + 1
> ReDim Preserve MyFiles(1 To Fnum)
> MyFiles(Fnum) = FilesInPath
> FilesInPath = Dir()
> Loop
>
>
> 'Loop through all files in the array(myFiles)
> If Fnum > 0 Then
> For Fnum = LBound(MyFiles) To UBound(MyFiles)
> Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
> mybook.Worksheets(1).Copy after:= _
> basebook.Sheets(basebook.Sheets.Count)
>
>
> On Error Resume Next
> ActiveSheet.Name = mybook.Name
> On Error GoTo 0
>
>
> ' You can use this if you want to copy only the values
> ' With ActiveSheet.UsedRange
> ' .Value = .Value
> ' End With
>
>
> mybook.Close savechanges:=False
> Next Fnum
> End If
> CleanUp:
> Application.ScreenUpdating = True
> End Sub
>
>
>
> --
> Regards Ron de Bruin
> http://www.rondebruin.nl
>
>
>
> "Chris Lewis" <(E-Mail Removed)> wrote in message news:45237d7a$(E-Mail Removed)...
> > Can anyone help me. I want to convert a number of csv files (all in the same folder) into an excel file. I would like each csv
> > to be a worksheet in the excel file with the sheet name the same as the csv file name but without the .csv file extension.
> >
> > I guess it would be easy if I was prompted for which folder I want to use as source as I have to do this over and over again!!
> >
> > --
> > Chris Lewis
> >


 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      5th Oct 2006
Hi Mustang

The OP want a different sheet for each csv file.
This will not work then

Here is a way to do your example with code
http://www.rondebruin.nl/csv.htm


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Mustang" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Try windows ms-dos
>
> in folder where the file are: example c:\ myfolder
> write, copy *.csv to c:\ all.csv
> and next.
>
> In excel import c:\all.csv
>
> Best regard
>
> Andrew
> Ron de Bruin ha escrito:
>
>> Try this one Chris
>>
>> Use this macro then if the csv files are in C:\Data
>> More info here
>> http://www.rondebruin.nl/copy3.htm
>>
>>
>> Sub Example()
>> Dim MyPath As String
>> Dim FilesInPath As String
>> Dim MyFiles() As String
>> Dim SourceRcount As Long
>> Dim Fnum As Long
>> Dim mybook As Workbook
>> Dim basebook As Workbook
>>
>>
>> 'Fill in the path\folder where the files are
>> 'on your machine
>> MyPath = "c:\Data"
>>
>>
>> 'Add a slash at the end if the user forget it
>> If Right(MyPath, 1) <> "\" Then
>> MyPath = MyPath & "\"
>> End If
>>
>>
>> 'If there are no Excel files in the folder exit the sub
>> FilesInPath = Dir(MyPath & "*.csv")
>> If FilesInPath = "" Then
>> MsgBox "No files found"
>> Exit Sub
>> End If
>>
>>
>> On Error GoTo CleanUp
>>
>>
>> Application.ScreenUpdating = False
>> Set basebook = ThisWorkbook
>>
>>
>> 'Fill the array(myFiles)with the list of csv files in the folder
>> Fnum = 0
>> Do While FilesInPath <> ""
>> Fnum = Fnum + 1
>> ReDim Preserve MyFiles(1 To Fnum)
>> MyFiles(Fnum) = FilesInPath
>> FilesInPath = Dir()
>> Loop
>>
>>
>> 'Loop through all files in the array(myFiles)
>> If Fnum > 0 Then
>> For Fnum = LBound(MyFiles) To UBound(MyFiles)
>> Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
>> mybook.Worksheets(1).Copy after:= _
>> basebook.Sheets(basebook.Sheets.Count)
>>
>>
>> On Error Resume Next
>> ActiveSheet.Name = mybook.Name
>> On Error GoTo 0
>>
>>
>> ' You can use this if you want to copy only the values
>> ' With ActiveSheet.UsedRange
>> ' .Value = .Value
>> ' End With
>>
>>
>> mybook.Close savechanges:=False
>> Next Fnum
>> End If
>> CleanUp:
>> Application.ScreenUpdating = True
>> End Sub
>>
>>
>>
>> --
>> Regards Ron de Bruin
>> http://www.rondebruin.nl
>>
>>
>>
>> "Chris Lewis" <(E-Mail Removed)> wrote in message news:45237d7a$(E-Mail Removed)...
>> > Can anyone help me. I want to convert a number of csv files (all in the same folder) into an excel file. I would like each
>> > csv
>> > to be a worksheet in the excel file with the sheet name the same as the csv file name but without the .csv file extension.
>> >
>> > I guess it would be easy if I was prompted for which folder I want to use as source as I have to do this over and over again!!
>> >
>> > --
>> > Chris Lewis
>> >

>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert multiple excel files with multiple sheets to PDF - how? Phil J W Microsoft Excel Misc 5 5th Nov 2009 07:43 PM
Convert one .csv file to multiple excel files singh Microsoft Excel Misc 1 9th Oct 2007 01:02 AM
need to convert 34 excel files into one access file =?Utf-8?B?bGVhcm5pbmdhY2Nlc3M=?= Microsoft Access External Data 8 8th Jul 2007 06:45 PM
How do I convert multiple Quattro Pro files to Excel? =?Utf-8?B?T0RPVCBKYWNr?= Microsoft Excel Misc 2 6th Oct 2004 04:00 PM
Re: How do I convert multiple Lotus 1-2-3 files to excel 2002? Frank Kabel Microsoft Excel Misc 2 9th Sep 2004 12:08 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:06 AM.