ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   Macro to Read Multiple CSV files into Excel (https://www.excelbanter.com/excel-worksheet-functions/265564-macro-read-multiple-csv-files-into-excel.html)

Kirstin Donaldson

Macro to Read Multiple CSV files into Excel
 
Hi,

I've written the macro below to read in and append several csv files together in excel. It works but can anyone tell me how I alter it to read only the header row from the first file?

Thanks
Kirstin

Sub List_All_The_Files_Within_Path()
Dim File_Path As String
Dim cell_to_paste_next_dataset As Range
Dim active_workbook, dataset_workbook As Workbook
Dim active_sheet As Worksheet
Dim file_count As Integer

Application.DisplayAlerts = False

'SPECIFY LOCATION of CSV FILES
File_Path = "S:\Data"

Set cell_to_paste_next_dataset = Cells(1, 1)
Set active_workbook = ActiveWorkbook
Set active_sheet = ActiveSheet
With Application.FileSearch
.NewSearch
.LookIn = File_Path
.Filename = "*.csv" 'CHANGE FILE EXTENSION TO OTHER EXCEL BASED EXTENSION IF YOU WANT
.SearchSubFolders = False
.Execute
For file_count = 1 To .FoundFiles.Count
If File_Path & "\" & active_workbook.Name < .FoundFiles(file_count) Then
Workbooks.Open Filename:=.FoundFiles(file_count)
Set dataset_workbook = ActiveWorkbook
Range(ActiveCell.SpecialCells(xlLastCell), Cells(2)).Copy
active_sheet.Activate
Cells(ActiveCell.SpecialCells(xlLastCell).Row, 1).Select
ActiveSheet.Paste
dataset_workbook.Close
End If
Next file_count
End With
End Sub


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com