+ Reply to Thread
Results 1 to 3 of 3

Run command on each Excel file in a specific folder

  1. #1
    Registered User
    Join Date
    12-26-2018
    Location
    Missoula, Montana
    MS-Off Ver
    2010
    Posts
    2

    Run command on each Excel file in a specific folder

    Hi there! I'm pretty new to VBA, and I'm trying to automate our time tracking process for one of our departments as much as I can. What I'd like to do is run a command from our main payroll prep Excel file (File A, for simplicity) that will open the first person's time sheet in a given folder (File B1), and copy the necessary values into a table from a separate file (File C) that will later be uploaded into our payroll system. Then File B1 will close, and File B2 will open for the next employee's hours to be copied into File C.

    Can anyone help with just the first few steps on how to get the loop started to open one file at a time and copy the values, taking into consideration that all the files will have unique names for each employee and each pay period (i.e. the file names in coding can't be static because they change each pay period).

    Thanks, everyone!

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,913

    Re: Run command on each Excel file in a specific folder

    Hi and welcome

    The below should get you started with looping through each Excel file in a specified folder.
    Using Dir it will not matter what the file names are as long as they have an Excel file extension of one type or another.
    PHP Code: 
    Private Sub ImportDataFromAllWorkbooksInAFolder()
        
    Dim FolderName As String
        Dim FileName 
    As String

        FolderName 
    "C:\Folder\Sub Folder" 'AMEND THIS TO YOUR RELEVANT FOLDER PATH
        FileName = Dir(FolderName & "*.xl??")
        
        Do While FileName <> ""
            Workbooks.Open (FolderName & FileName)
            '
    CODE HERE FOR COPYING DETAILS FROM EACH WORKBOOK INTO YOUR MASTER WORKBOOK
            Workbooks
    (FileName).Close
            FileName 
    Dir()
        
    Loop
    End Sub 
    Impossible to give more specific code on how to copy details from the individual workbooks into your master one without some more detail on exactly what needs copying. Perhaps a sample file (desensitized) showing what needs copying from and where it needs copying too will help with that.

    Let us know how you get on.

    BSB

  3. #3
    Registered User
    Join Date
    12-26-2018
    Location
    Missoula, Montana
    MS-Off Ver
    2010
    Posts
    2

    Re: Run command on each Excel file in a specific folder

    Thank you, BSB! After thinking this through (the hard way, of course), I'm thinking it would make the most sense to just copy the files (all with just one tab each) over to a new workbook. Is that something I can find a simple code to run?

    Thank you again!

+ 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. [SOLVED] Vba code to compile specific details from different excel file to one file from folder
    By UPA in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-25-2017, 07:38 AM
  2. [SOLVED] Open newest folder and specific file in the folder
    By timtim91 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-09-2017, 11:55 AM
  3. [SOLVED] Excel can't run an existing specific EXE file on system32 folder !! HELP PLZ
    By nkiine in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 02-27-2017, 05:54 AM
  4. Search a word file in a specific folder and copy text to excel
    By corinereyes in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-02-2012, 08:15 AM
  5. Macro for copying specific data from excel files in a folder to a newly created excel file
    By Raju Radhakrishnan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-06-2012, 02:24 PM
  6. Finding a specific Excel file Name from a folder through VBA
    By daksh1981 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-30-2010, 06:56 AM
  7. Opening specific file or folder from Excel
    By goranm in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-12-2010, 08:53 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