+ Reply to Thread
Results 1 to 5 of 5

VBA for multiple workbooks in a directory

Hybrid View

jone kim VBA for multiple workbooks in... 07-30-2014, 12:55 PM
Marc L Re: VBA for multiple... 07-30-2014, 12:59 PM
jone kim Re: VBA for multiple... 07-30-2014, 09:09 PM
cemregunay Re: VBA for multiple... 07-30-2014, 09:13 PM
cemregunay Re: VBA for multiple... 07-30-2014, 09:17 PM
  1. #1
    Registered User
    Join Date
    07-16-2014
    Location
    kathmandu, nepal
    MS-Off Ver
    2007
    Posts
    15

    VBA for multiple workbooks in a directory

    Hey Guys,

    Let me explain what I am trying to achieve:

    I've multiple excel workbooks in a folder. I've to do same task in all of the work books.

    Running the same script from Visual Basic Editor for each & every excel work book is very tedious.

    How can I run the VBA code for every workbooks in a directory?

    Suppose pass a parameter as folder name with path and the VBA code executes for every excel work book present there.

    Is this possible? While surfing I found that we can use Visual Basic Script in such cases, I dont have any idea to do this,
    please explain.

    Thank You!!!

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: VBA for multiple workbooks in a directory


    Hi,

    should be possible if you know coding in VBA !

    Start to read Dir function inner VBA help …

  3. #3
    Registered User
    Join Date
    07-16-2014
    Location
    kathmandu, nepal
    MS-Off Ver
    2007
    Posts
    15

    Re: VBA for multiple workbooks in a directory

    Hello Marc L,

    I think this is also possible using Visual Basic Script. But I am not sure.

    Thank You.

  4. #4
    Registered User
    Join Date
    07-17-2014
    Location
    Turkey
    MS-Off Ver
    2003
    Posts
    85

    Re: VBA for multiple workbooks in a directory

    VBA code to open multiple files in same folder and perform same macros

    Sub OpenAndClearContentsInColumnAA()
    Dim strF As String, strP As String
    Dim wb As Workbook
    Dim ws As Worksheet
    
    
    'Edit this declaration to your folder name
    strP = "C:\Documents and Settings\MANAGER\Desktop\Dazzas Files" 'change for the path of your folder
    
    
    strF = Dir(strP & "\*.xlsm") 'Change as required
    
    
    Do While strF <> vbNullString
    
    
        Set wb = Workbooks.Open(strP & "\" & strF)
        Set ws = wb.Sheets(1) 'uses first sheet or if all the same names then ws.Sheets("yoursheet")
        ws.Range("AA2:AA" & ws.Range("AA" & ws.Rows.Count).End(xlUp).Row).ClearContents
        wb.Close True
        
        strF = Dir()
    Loop
    
    
    End Sub
    and another VBA Code To Open Multiple Excel Files;

    Const FOLDER As String = "C:\\test\" 
     
    Sub ProcessEachFileInFolder() 
         
        On Error Goto ErrorHandler 
         
        Dim fileName As String 
         
        fileName = Dir(FOLDER, vbDirectory) 
         
         ' loop through folder, only process .xls files
        Do While Len(fileName) > 0 
            If Right$(fileName, 4) = "xlsb" Then 
                Call ProcessFile(fileName) 
            End If 
            fileName = Dir 
        Loop 
         
    ProgramExit: 
        Exit Sub 
    ErrorHandler: 
        MsgBox Err.Number & " - " & Err.Description 
        Resume ProgramExit 
    End Sub 
     
    Sub ProcessFile(fileName As String) 
         
        Dim currentWkbk As Excel.Workbook 
         
         ' open workbook
        Set currentWkbk = Excel.Workbooks.Open(FOLDER & fileName) 
         
         ' do whatever you need to do with currentWkbk
         
    End Sub

  5. #5
    Registered User
    Join Date
    07-17-2014
    Location
    Turkey
    MS-Off Ver
    2003
    Posts
    85

    Re: VBA for multiple workbooks in a directory

    If you want to apply all excel files ( including closed );

    Sub ProcessFiles()
        Dim Filename, Pathname As String
        Dim wb As Workbook
    
        Pathname = ActiveWorkbook.Path & "\Files\"
        Filename = Dir(Pathname & "*.xls")
        Do While Filename <> ""
            Set wb = Workbooks.Open(Pathname & Filename)
            DoWork wb
            wb.Close SaveChanges:=True
            Filename = Dir()
        Loop
    End Sub
    
    Sub DoWork(wb As Workbook)
        With wb
            'Do your work here
            .Worksheets(1).Range("A1").Value = "Hello World!"
        End With
    End Sub

+ 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. Open multiple workbooks in directory, copy a range to them for data collection
    By agutman50 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-28-2014, 01:24 PM
  2. Sort first column of multiple workbooks from a single directory
    By lmolokin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-08-2012, 05:15 PM
  3. Replies: 0
    Last Post: 01-21-2012, 11:48 PM
  4. Copying range into multiple workbooks in a directory
    By tinkerbelle in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2007, 04:43 AM
  5. header onto multiple workbooks in same directory
    By AmyTaylor in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-13-2005, 07:17 AM

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