+ Reply to Thread
Results 1 to 9 of 9

Macro that changes a cell in each file within a folder

Hybrid View

  1. #1
    Registered User
    Join Date
    09-26-2009
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2003
    Posts
    12

    Macro that changes a cell in each file within a folder

    Hello,
    I'm having a bugger of a time with my macro (pasted below). The macro is supposed to be written to make changes in each excel file within a folder. Specifically I want to copy paste a value from one worksheet to another (in the same file) and then save and close. The idea is to repeat this for all of the 3,000 files in the folder.

    When I run the macro, the hour glass starts to work and after about 20 seconds or so it stops and no changes were made. I am attaching the file that has the macro in it, as well as one of the 3,000 files that I am trying to change.

    Any help would be appreciated.
    Thanks from Seattle,
    Peter

    Sub ProAddJobCostingDate()
    With Application.FileSearch
    .LookIn = "F:\Excel"
    .Filename = "*.xls"
    .FileType = msoFileTypeExcelWorkbooks
    If .Execute() > 0 Then
    
    Set newwkbk = ActiveWorkbook
    
    For i = 1 To .FoundFiles.Count
    Set wkbk = Workbooks.Open(.FoundFiles(i))
    Call Sheets("Individuals").Select
        Range("f1").Select
        Selection.Copy
        Sheets("JobCosting").Select
        Range("l1").Select
        ActiveSheet.Paste
        ActiveWorkbook.save
        ActiveWorkbook.Close
    
    Next i
    End If
    End With
    End Sub
    Attached Files Attached Files

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

    Re: Macro that changes a cell in each file within a folder

    Do the workbooks have macros? Do they take a long time to calculate?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    09-26-2009
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Macro that changes a cell in each file within a folder

    None of the workbooks have their own macros. The only macro is in the Crew99999 file (one that was attached to this posing). I keep it in the same folder as the other (3,000) files and use it to run all of the other files in the folder. I don't know if this is the "right" way to do things, but I do not know any other way.

    -Peter

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

    Re: Macro that changes a cell in each file within a folder

    Try this:
    Sub ProAddJobCostingDate()
        Dim i As Long
        
        With Application.FileSearch
            .LookIn = "F:\Excel"
            .Filename = "*.xls"
            .FileType = msoFileTypeExcelWorkbooks
            
            If .Execute() > 0 Then
                For i = 1 To .FoundFiles.Count
                    Workbooks.Open .FoundFiles(i)
                    Worksheets("Individuals").Range("F1").Copy _
                        Destination:=Worksheets("JobCosting").Range("L1")
                    ActiveWorkbook.Close Savechanges:=True
                Next i
            End If
        End With
    End Sub
    If that works, you can turn ScreenUpdating off during execution to speed things up.

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro that changes a cell in each file within a folder

    I'm only guessing what you're attempting to do based on a step through of the macro in WB 01-01-01#Crew 9999.xls.
    It appears as if you're trying to copy the date from Cell F1 of WB 01-01-01#Crew 9999.xls (Week Ending: 2/18/2006) to WB 01-07-06#Crew 23.xls, WS Individuals Cell F1 and WS JobCosting Cell L1.

    If this is what you're attempting to do, try this code
    Option Explicit
    
    Sub ProAddJobCostingDate()
        Dim i As Long
        Dim Wb1 As Workbook
    
        Set Wb1 = ActiveWorkbook
        With Application.FileSearch
    
            .LookIn = "F:\Excel"
            .Filename = "*.xls"
            .FileType = msoFileTypeExcelWorkbooks
    
            If .Execute() > 0 Then
                For i = 1 To .FoundFiles.Count
    
                    If (.FoundFiles(i)) <> .LookIn & "\" & ActiveWorkbook.Name Then
    
                        Workbooks.Open .FoundFiles(i)
                        Wb1.Worksheets("Individuals").Range("F1").Copy _
                                Destination:=Worksheets("JobCosting").Range("L1")
                        Wb1.Worksheets("Individuals").Range("F1").Copy _
                                Destination:=Worksheets("Individuals").Range("F1")
    
                        ActiveWorkbook.Close Savechanges:=True
                    End If
                Next i
            End If
        End With
    End Sub
    If this is not what you're after, let me know. J
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  6. #6
    Registered User
    Join Date
    09-26-2009
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Macro that changes a cell in each file within a folder

    This worked. Thanks. I'm still new to VBA so I appreciate the help. It a whole new language, but one with tons of potential I am finding.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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