• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA on .csv file to run all files in one folder and give result in 1 sheet

ALAMZEB

Member
Hi
We get files in .CSV file and we run attached macro to arrange data (after running you will know how it looks)
There could be 30 .csv files in folder. I have to go one by one , run macros and than copy paste result to new sheet
Can I run macro that will search all files in folders than run macro on all of them and give me result in one file

Please change file extension from .xls to .csv
ALl macros are in file that is attached below
 

Attachments

  • DEAL_SHEET_K03984_08022015.xls
    1.3 KB · Views: 8
There were no macro's in the attached file
But this should be what your after

Code:
Sub Extract_CSV_Files()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
Dim SrcSht As Workbook

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\Huis\Desktop") 'Change directory to suit
i = 0

Set SrcSht = ActiveWorkbook

For Each objFile In objFolder.Files

  If UCase(Right(objFile, 3)) = "CSV" Then
  
  Workbooks.Open fileName:=objFile
  Sheets(1).Copy After:=Workbooks(SrcSht.Name).Sheets(SrcSht.Sheets.Count)
  
  Windows(objFile.Name).Close
  i = i + 1
  End If
Next objFile

MsgBox "Finished processing " + CStr(i) + " files."

End Sub
 
Sorry Depak & Hui
i thought i have attached macro in file. i will reattach shortly

Hui : thanks for above macro.i was just wondering where should i include my macros in your code above.
 
In a code module in VBA

See attached file:

Don't forget to change the Directory name
 

Attachments

  • Read text file contents CSV.xlsm
    15.1 KB · Views: 4
Last edited:
Thanks Hui
It works like a charm but I have reattached sheet with macro
I run this macro one by one on each csv file.This macro extracts the info and arrange them into correct format
Can I run my macro to run on all tabs after your macro is run and then gives me result in one tab?
I ran macro in first three sheets just to give you an idea
Sorry this macro is too large
 

Attachments

  • Read text file contents CSV.xlsm
    127.6 KB · Views: 2
See how this goes
I have cleaned up the code and deleted the Skippy and Code sheets
Your code sits inside a separate Subroutine in Module1 a Code Module
You can add a line Call MFG to the existing module at the end if you want to run it straight away

I have renamed the worksheets 1Deal so they don't get processed so you can test it

I can't test it as I don't have the lookup file

Everything sits inside a loop that processes all the worksheets
Checking that it isn't the Master Sheet or 1 1Deal sheet
 

Attachments

  • Read text file contents CSV2.xlsm
    128.4 KB · Views: 6
Hi Hui

if i run MFG Macro only by changing rest of tabs to 1Deal,the MFG Macro doesn't work.
What i was wondering that after your Extract CSV file macro runs, this MFG macros runs on all tabs from sheet and than save result in one tab.


currently i have to run MFG macro on one tab at a time and than copy all individual tabs result to separate file
 
In your previous post you said you had ran it on 3 sheets already
I renamed them, to 1Deal.... etc so they didn't get reformatted again
But it still ran on the sheets Deal....

If you look at the MFG macro it steps through each of the sheets 1 at a time and reformats them the same as the previous macro did

What results, or range, do you want transferred to a Summary sheet?
 
Hi Hui

when now i am running MFG macro on deal tabs it gives me error on
Range("E20").NumberFormat = "0.00"
Selection.AutoFill Destination:=Range("E20:E121")

about the result, MFG gives results from I19 on every tab (1Deal as example). can it gives result on one sheets of all tabs/deal tabs.
 
Why is it running on 1Deal it shouldn't be ?

The 1Deal sheets were the sheets you had already modified
 
Hi hui

Its not runnung on 1deal.i gave 1deal.just for final result layout
i triee to run in deal only but it didnt work and its not going through all deal tabs
 
The code I sent in the last file doesn't run the 1Deal worksheets
But runs the Deal Sheet only!

What have you done in between
can you repost your new file?
 
Hi Hui

I think when you changed MFG macro, it didnt work. i have attached new sheet with original Macro. If you run it on one deal sheet than you will know it works. but when you rewrite macro to rum on all deal tabs, it gave me some kind of error.
Let me know how you go & thanks Hui for doing all this
Much appreciated
 

Attachments

  • Read text file contents CSV2 NEW FILE.xlsm
    65 KB · Views: 1
please try this:
 

Attachments

  • Read text file contents CSV2 NEW FILE.xlsm
    57 KB · Views: 3
Hi Hui

its working now
as you can see in attachemnt that all deal tabs have their final result from column ! to column P.
can we summarise all that info into Master tab ignoring colors part
&
can we do this other way around such as
If ws.Name <> "Master" And UCase(Left(ws.Name, 5)) <> "1DEAL" Then

istead of not looking in 1DEAL, it should look only into 1Deal
 
At the Top of the MFG macro can you change the line:
From:
If ws.Name <> "Master" And UCase(Left(ws.Name, 5)) <> "1DEAL" Then

To:
If ws.Name <> "Master" Then

Then run the MFG Subroutine

Then Post the file here

I cannot run it as I don't have access to the metcash macro.XLSB file

Please highlight what data you want summarised on one of the worksheets
 
Hi Hui

I have explanied everything on attcahed sheet with example summary
let me know if you need anythig else
 

Attachments

  • Read text file contents CSV2 NEW FILE part 2.xlsm
    88.7 KB · Views: 1
There is a new Subroutine MakeSummary
Simply run that
Other comments in Red in your text box
 

Attachments

  • Read text file contents CSV2 NEW FILE part 2.xlsm
    82.6 KB · Views: 1
Thanks Hui

Comments regarding your enquiry in file
 

Attachments

  • Read text file contents CSV2 NEW FILE part 3.xlsm
    82.4 KB · Views: 1
see how this goes
 

Attachments

  • Read text file contents CSV2 NEW FILE part 3.xlsm
    85.3 KB · Views: 1
Hi Hui

MFG Macro doesnt work now.its giving me error
as
unable to get pivotitem property of the pivot fiels
i have attacjed macro file as well so you can run

Please change extension of macro file from .xls to .xlsb
 

Attachments

  • Read text file contents CSV2 NEW FILE part 4.xlsm
    55.1 KB · Views: 1
  • metcash macro.xls
    81 KB · Views: 3
Back
Top