import multiple csv files

lomri

New Member
Joined
Jan 28, 2018
Messages
2
hi,
I found a neat code to import multiple csv files into one worksheet.
The problem I encountered is that I want to skip the first 2 lines of each csv. I tried searching everywhere how to do it but couldn't find what I was looking for.



Code:
[COLOR=#5F6A72][FONT=&quot]Option Explicit[/FONT][/COLOR]

[COLOR=#5F6A72][FONT=&quot]Sub ImportCSVsWithReference()[/FONT][/COLOR]
[B]'Author:    Jerry Beaucaire
'Date:      10/16/2010
'Summary:   Import all CSV files from a folder into a single sheet
'           adding a field in column A listing the CSV filenames

[COLOR=#5F6A72]Dim wbCSV   As Workbook[/COLOR]
[COLOR=#5F6A72]Dim wsMstr  As Worksheet:   Set wsMstr = ThisWorkbook.[/COLOR][B][COLOR=#CC0000]Sheets("MasterCSV")[/COLOR]
Dim fPath   As String:      fPath = [B][COLOR=#CC0000]"C:\2010\Import\"[/COLOR]    [B]'path to CSV files, include the final \
[COLOR=#5F6A72]Dim fCSV    As String[/COLOR]

[COLOR=#5F6A72]If MsgBox("Clear the existing MasterCSV sheet before importing?", vbYesNo, "Clear?") _[/COLOR]
[COLOR=#5F6A72]    = vbYes Then wsMstr.UsedRange.Clear[/COLOR]

[COLOR=#5F6A72]Application.ScreenUpdating = False  [/COLOR][B]'speed up macro

[COLOR=#5F6A72]fCSV = Dir(fPath & [/COLOR][B]"*.csv"[COLOR=#5F6A72])         [/COLOR][B]'start the CSV file listing

[COLOR=#5F6A72]    Do While Len(fCSV) > 0[/COLOR]
[B]      'open a CSV file
[COLOR=#5F6A72]        Set wbCSV = Workbooks.Open(fPath & fCSV)[/COLOR]
[B]      'insert col A and add CSV name
[COLOR=#5F6A72]        Columns(1).Insert xlShiftToRight[/COLOR]
[COLOR=#5F6A72]        Columns(1).SpecialCells(xlBlanks).Value = ActiveSheet.Name[/COLOR]
[B]      [COLOR=#38761D]'copy date into master sheet and close source file[/COLOR]
[COLOR=#5F6A72]        ActiveSheet.UsedRange.Copy wsMstr.Range("A" & Rows.Count).End(xlUp).Offset(1)[/COLOR]
[COLOR=#5F6A72]        wbCSV.Close False[/COLOR]
[B]      [COLOR=#38761D]'ready next CSV[/COLOR]
[COLOR=#5F6A72]        fCSV = Dir[/COLOR]
[COLOR=#5F6A72]    Loop[/COLOR]
[COLOR=#5F6A72] [/COLOR]
[COLOR=#5F6A72]Application.ScreenUpdating = True[/COLOR]
[COLOR=#5F6A72]End Sub[/COLOR][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B]
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi & welcome to MrExcel
Try
Code:
ActiveSheet.UsedRange.Offset(2).Copy wsMstr.Range("A" & Rows.Count).End(xlUp).Offset(1)
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,742
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top