I am very new to excel macros.
Could anyone help me to create a macro to merge all *.csv files in one folder to a single new excel file in other folder?
Thanks a lot in advance...
I am very new to excel macros.
Could anyone help me to create a macro to merge all *.csv files in one folder to a single new excel file in other folder?
Thanks a lot in advance...
Try this code
Copy the Excel VBA code![]()
Option Explicit Sub cons_data() Dim Master As Workbook Dim sourceBook As Workbook Dim sourceData As Worksheet Dim CurrentFileName As String Dim myPath As String Application.ScreenUpdating = False 'The folder containing the files to be recap'd myPath = "B:\Test" 'Finds the name of the first file of type .xls in the current directory CurrentFileName = Dir(myPath & "\*.csv") 'Create a workbook for the recap report Set Master = ThisWorkbook Do Workbooks.Open (myPath & "\" & CurrentFileName) Set sourceBook = Workbooks(CurrentFileName) Set sourceData = sourceBook.Worksheets(1) With sourceData lrow = .Range("A" & .Rows.Count).End(xlUp).Row .Rows("2:" & lrow).Copy Master.Worksheets(1).Range("A" & Rows.Count).End(xlUp).Offset(1, 0) End With sourceBook.Close 'Calling DIR w/o argument finds the next .xlsx file within the current directory. CurrentFileName = Dir() Loop While CurrentFileName <> "" MsgBox "Consolidation complete" Application.ScreenUpdating = True End Sub
Select the workbook in which you want to store the Excel VBA code
Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
Choose Insert | Module
Where the cursor is flashing, choose Edit | Paste
To run the Excel VBA code:
Choose View | Macros
Select a macro in the list, and click the Run button
If I have helped, Don't forget to add to my reputation (click on the star below the post)
Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
Use code tags when posting your VBA code: [code] Your code here [/code]
Thanks a lot for the code. But I got a "Compile error: Variable not defined" message, and stopped on "lrow = ..." line.
Could you help me out again. - Thanks again
Oops. Just add dim lrow as long at the end of all the dim statements.
Thanks, no more compile error. however I didn't get a result that I was looking for. Maybe I didn't explain well on my expected result.
I am trying to read all *.csv file in folder and put in one excel file as worksheet per .csv file.
So, if I read 5 csv files, I would expect 5 sheets with each csv file name in the excel file.
Would that be possible? Thanks a lot for you help
Updated code -
![]()
Option Explicit Sub cons_data() Dim Master As Workbook Dim sourceBook As Workbook Dim sourceData As Worksheet Dim CurrentFileName As String Dim myPath As String Dim sname As String Application.ScreenUpdating = False 'The folder containing the files to be recap'd myPath = "B:\Test" 'Finds the name of the first file of type .xls in the current directory CurrentFileName = Dir(myPath & "\*.csv") 'Create a workbook for the recap report Set Master = ThisWorkbook Do Workbooks.Open (myPath & "\" & CurrentFileName) Set sourceBook = Workbooks(CurrentFileName) Set sourceData = sourceBook.Worksheets(1) With sourceData sname = Left(CurrentFileName, Len(CurrentFileName) - 4) Master.Worksheets.Add(after:=Master.Worksheets(Worksheets.Count)).Name = sname .Cells.Copy Master.Worksheets(sname).Range("A1") End With sourceBook.Close 'Calling DIR w/o argument finds the next .xlsx file within the current directory. CurrentFileName = Dir() Loop While CurrentFileName <> "" MsgBox "Consolidation complete" Application.ScreenUpdating = True End Sub
Works great! thanks a lot for your fast response
Based on your last post in this thread, its apparent that you are satisfied with the solution(s) you've received and have solved your question, but you haven't marked your thread as "SOLVED". I will do it for you this time.
In future, to mark your thread as Solved, you can do the following -
Select Thread Tools-> Mark thread as Solved.
Incase your issue is not solved, you can undo it as follows -
Select Thread Tools-> Mark thread as Unsolved.
Thanks for the rep.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks