+ Reply to Thread
Results 1 to 4 of 4

Opening multiple files and applying a macro to all of them

  1. #1
    Registered User
    Join Date
    09-04-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Question Opening multiple files and applying a macro to all of them

    Hi guys,
    I'm new in VBA, and I have tried for several days to tackle with my problem by searching in internet, but now it seems I need the professionals help ...
    There are more than 100 excel files (*.csv) in different folders. Each excel file contains:
    3 columns [A-Date, B-hour (unnecessary), C-Value],
    1st row is description line,
    and 2nd row is col. header (data should be read from 3rd row)


    Data is stored daily for several years. What I want to do is make it monthly for each year.

    So, the description of the macro is like this:
    Open the excel file. "Sheet1" is renamed.
    Change format of 1st col. (A-Date) to DMY.
    Calculate summation for each month. (summation of values in col.C)
    Write the results in another excel file in following format:

    Please refer to the image.

    9-4-2013 9-25-05 PM.jpg


    'deploy a macro on several excel files in a folder'
    Sub main_sub()

    Set ThisWB = ActiveWorkbook 'to refer to the original excel'
    PathName = "C:\excel macro test\"
    Filename = Dir(PathName & "*.csv")
    Do While Filename <> ""
    Set CurrentWorkbook = Workbooks.Open(PathName & Filename) 'to refer to and write in the newly open file'
    ' do your stuff
    Call a
    Call SortMonthly
    Filename = Dir()
    Loop


    End Sub


    Sub a()
    '
    ' a Macro
    '
    ' Keyboard Shortcut: Ctrl+g
    '
    'Range("A3:A").Select
    'Keyboard Shortcut: Ctrl g
    '
    Range("A3:A21100").Select
    Selection.TextToColumns Destination:=Range("A3"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
    :=Array(1, 4), TrailingMinusNumbers:=True
    End Sub

    Sub SortMonthly()

    SheetName = ActiveSheet.Name
    i = 3
    Sm = 0
    r = 4
    Fin = True
    YR = ActiveSheet.Range("A" & i)
    ActiveSheet.Range("E" & i).Select
    ActiveCell.FormulaR1C1 = YR

    While Fin



    While YR = Year(ActiveSheet.Range("A" & i))
    M = Month(ActiveSheet.Range("A" & i))


    While Month(ActiveSheet.Range("A" & i)) = M
    Sm = Sm + ActiveSheet.Range("C" & i)
    i = i + 1
    Wend

    ThisWB.Worksheets("Sheet1").Range("E" & r).Select
    ActiveCell.FormulaR1C1 = Sm
    Mo = MonthName(M)
    ThisWB.Worksheets("Sheet1").Range("D" & r).Select
    ActiveCell.FormulaR1C1 = Mo
    r = r + 1
    Sm = 0
    Wend

    r = r + 1
    YR = Year(ActiveSheet.Range("A" & i))
    ActiveSheet.Range("E" & r).Select
    ActiveCell.FormulaR1C1 = YR
    r = r + 1

    If ActiveSheet.Range("A" & i) = "" Then Fin = False

    Wend

    End Sub

    'continue with loop in folder
    Thank you
    Mori

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Opening multiple files and applying a macro to all of them

    attach please a sample csv file and xls with desired result
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    09-04-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Opening multiple files and applying a macro to all of them

    Files attached.
    Thank you
    Attached Images Attached Images
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Opening multiple files and applying a macro to all of them

    Quote Originally Posted by patel45 View Post
    attach please a sample csv file and xls with desired result
    is not clear ?

  5. #5
    Registered User
    Join Date
    09-04-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Opening multiple files and applying a macro to all of them

    Done. If sth needed pls let me know.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Help me with a Macro to extract data from multiple files without opening them?
    By matador_24 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-03-2011, 03:23 PM
  2. Excel 2007 : Applying macros to multiple files?
    By soultrain in forum Excel General
    Replies: 0
    Last Post: 12-06-2010, 06:57 PM
  3. Opening files macro help (forcing directory, opening a tsv file)
    By NukedWhale in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-10-2010, 03:07 PM
  4. Applying macro to 'all opened files'
    By Pedroluna in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-21-2009, 02:12 PM
  5. Applying XLT Template to multiple files
    By Telmet320 in forum Excel General
    Replies: 2
    Last Post: 09-25-2008, 10:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1