+ Reply to Thread
Results 1 to 15 of 15

Loop a macro through the multiple files

Hybrid View

  1. #1
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Loop a macro through the multiple files

        Set wkbCsv = Application.Workbooks.Open(Filename:=strFOLD & strFile)
    Entia non sunt multiplicanda sine necessitate

  2. #2
    Registered User
    Join Date
    11-08-2014
    Location
    Malaysia
    MS-Off Ver
    2010
    Posts
    84

    Re: Loop a macro through the multiple files

    Quote Originally Posted by shg View Post
        Set wkbCsv = Application.Workbooks.Open(Filename:=strFOLD & strFile)
    Yes. It works. Thank you.
    There is another problem.
    My original recorded macro can do the work completely for one file, but I can not put Step 5 in the loop in polished version of macro (next code).

    'Step5: Inserting FileName as "ID" in ColA until the last row of data
             
         Range("A2").Select
         ActiveWorkbook.Names.Add Name:="ID", RefersToR1C1:="=GET.DOCUMENT(88)"
         ActiveWorkbook.Names("ID").Comment = ""
         ActiveCell.FormulaR1C1 = "=SUBSTITUTE(ID, "".csv"", """")"
         Range("A2").Select
         
         Dim Lr     As Long
         Lr = Cells.Find("*", , , , 1, 2).Row
         If Selection(1).Row < Lr Then
            Selection.AutoFill Destination:=Selection.Resize(Lr - Selection.Row + 1)
         End If
         
    This one is the polished one. The step 5 is not included in this code.
    Sub Macro_data_L3()
    
        ' Set folder containing CSV files
        Const strFOLD As String = "C:\Users\test\New folder\"
        
        ' Set formula to be used
        Const strFORMULA As String = "GET.DOCUMENT(88)"
        
        Dim strFile As String
        Dim wkbCsv As Workbook
        Dim lngLastRow As Long
        
        strFile = Dir$(strFOLD & "*.csv", vbNormal)
        
        Do While strFile <> vbNullString
            Set wkbCsv = Application.Workbooks.Open(Filename:=strFOLD & strFile)
            
            With wkbCsv.Worksheets(1) 'step1: Deleting 1st row
                .Rows("1").Delete
                
                With .Columns("B")
                    .Clear
                    .Insert
                End With
        'Step2: TXT2Col
                .Columns("A").TextToColumns _
                    Destination:=.Range("A1"), _
                    Other:=True, _
                    OtherChar:=Chr(47), _
                    FieldInfo:=Array( _
                        Array(1, xlGeneralFormat), _
                        Array(2, xlGeneralFormat), _
                        Array(3, xlGeneralFormat))
         'Step3: Changing cell format to DMY
                .Columns("A:C").ClearFormats
                .Range("A1:C1").Value = Array("Day", "Month", "Year")
         'Ste4: Swapping Columns
                .Columns("C").Cut
                .Columns("A").Insert
                .Columns("B").Cut
                .Columns("D").Insert
                .Columns("A").Insert
         
        
            ' Step 5 
                .Range("A1").Value = "ID"
                lngLastRow = .Range("B" & .Rows.Count).End(xlUp).Row
                
                .Range("A2:A" & lngLastRow).Formula = _
                    "=SUBSTITUTE(" & strFORMULA & ", "".CSV"", """")"
            
     ' This part is incomplete !      
                          
                strFile = Dir$()
            End With
        Loop
    
    End Sub
    Last edited by Moriexcel; 11-23-2014 at 06:42 AM.

+ 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. Macro to open multiple files, remove header and save multiple files in a new format
    By twocircles in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-17-2014, 05:24 PM
  2. Produce multiple PDF files from multiple Excel Files
    By yukipilas in forum Excel General
    Replies: 2
    Last Post: 09-02-2013, 06:52 AM
  3. split text files stored in some location to multiple files based on a condition
    By GIRISH_KH in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-01-2013, 11:32 AM
  4. [SOLVED] Macro to Import Multiple TXT Files into workbook - User to select files/directory
    By saber007 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-15-2013, 08:43 PM
  5. Replies: 0
    Last Post: 11-27-2012, 01:43 PM

Tags for this Thread

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