Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Mintz87
 
Posts: n/a
Default Import CSV files using Macro

i have 41 csv files that i need to import into 41 sheets in Excel. the name
of the csv files and the sheet names are the same minus the extension (.csv).
The sheets will be existing sheets with headers. Please Help the Novice

thanks

M3ntz
  #2   Report Post  
Duke Carey
 
Posts: n/a
Default

Well, one way would be to use the Data-Import External Data-New Database
Query and select CSV files as the source database.

Drawback: it's completey manual & you'll have to go through it 41 times

Another way is to choose File-Open, select all 41 CSV files, and let Excel
open them. Each will open in its own workbook. You can then copy and paste
from each CSV workbook to the appropriate place in your existing sheets.

Drawback: it's still lots of manual work, but should go quicker than the
first option.

Third way - write VBA code to open each CSV file in turn and copy the data
into the correct existing sheet.

"Mintz87" wrote:

i have 41 csv files that i need to import into 41 sheets in Excel. the name
of the csv files and the sheet names are the same minus the extension (.csv).
The sheets will be existing sheets with headers. Please Help the Novice

thanks

M3ntz

  #3   Report Post  
Mintz87
 
Posts: n/a
Default

Do you have the code for the VBA way of doing this? I have to keep this as
automated as possible. thanks

"Duke Carey" wrote:

Well, one way would be to use the Data-Import External Data-New Database
Query and select CSV files as the source database.

Drawback: it's completey manual & you'll have to go through it 41 times

Another way is to choose File-Open, select all 41 CSV files, and let Excel
open them. Each will open in its own workbook. You can then copy and paste
from each CSV workbook to the appropriate place in your existing sheets.

Drawback: it's still lots of manual work, but should go quicker than the
first option.

Third way - write VBA code to open each CSV file in turn and copy the data
into the correct existing sheet.

"Mintz87" wrote:

i have 41 csv files that i need to import into 41 sheets in Excel. the name
of the csv files and the sheet names are the same minus the extension (.csv).
The sheets will be existing sheets with headers. Please Help the Novice

thanks

M3ntz

  #4   Report Post  
Duke Carey
 
Posts: n/a
Default

This is mostly plagiarized from Excel's VBA help file

It assumes 1) that all your 41 named tabs are in the same workbook, 2) you
paste this code into a module in that workbook & run the code when that
workbook is active, 3) the CSV files are in c:\CSV files\ and 4) you are
going to paste the data starting with cell A2

For help on where & how to paste VBA code:

http://www.cpearson.com/excel/codemods.htm

For information on installing the code see
Getting Started with Macros and User Defined Functions

http://www.mvps.org/dmcritchie/excel/getstarted.htm

Here's the code
---------------------------------------------------------------------------
Sub OpenCSV()
Dim i As Integer
' change this next line to reflect the actual directory
Const strDir = "c:\csv files\"
Dim ThisWB As Workbook
Dim wb As Workbook
Dim ws As Worksheet
Dim strWS As String

Set ThisWB = ActiveWorkbook

Set fs = Application.FileSearch
With fs
.LookIn = strDir
.Filename = "*.csv"
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) 0 Then
For i = 1 To .FoundFiles.Count
Set wb = Workbooks.Open(.FoundFiles(i))
strWS = wb.Sheets(1).Name
wb.Sheets(1).UsedRange.Copy (ThisWB.Worksheets(strWS).Range("A2"))
wb.Close False
Next i
Else
MsgBox "There were no files found."
End If
End With

End Sub
-----------------------------------------------------------------------------------------------------------


"Mintz87" wrote:

Do you have the code for the VBA way of doing this? I have to keep this as
automated as possible. thanks

"Duke Carey" wrote:

Well, one way would be to use the Data-Import External Data-New Database
Query and select CSV files as the source database.

Drawback: it's completey manual & you'll have to go through it 41 times

Another way is to choose File-Open, select all 41 CSV files, and let Excel
open them. Each will open in its own workbook. You can then copy and paste
from each CSV workbook to the appropriate place in your existing sheets.

Drawback: it's still lots of manual work, but should go quicker than the
first option.

Third way - write VBA code to open each CSV file in turn and copy the data
into the correct existing sheet.

"Mintz87" wrote:

i have 41 csv files that i need to import into 41 sheets in Excel. the name
of the csv files and the sheet names are the same minus the extension (.csv).
The sheets will be existing sheets with headers. Please Help the Novice

thanks

M3ntz

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
import data with macro chris_rip Excel Discussion (Misc queries) 0 July 18th 05 08:40 PM
import data using macro chris_rip Excel Discussion (Misc queries) 0 July 18th 05 08:32 PM
Import data from files with different names to EXCEL D'base. thunderfoot Excel Discussion (Misc queries) 2 June 10th 05 11:05 AM
Macro Help- combining "CS" files Judyt Excel Discussion (Misc queries) 1 February 17th 05 02:19 AM
How do I record a macro which should work on multiple files ? Venkataraman.P.E Excel Discussion (Misc queries) 2 January 16th 05 10:26 AM


All times are GMT +1. The time now is 10:54 AM.

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

About Us

"It's about Microsoft Excel"