+ Reply to Thread
Results 1 to 6 of 6

Loop Through all files in a folder and use the same macro to edit each one.

Hybrid View

dj59 Loop Through all files in a... 09-17-2012, 04:04 PM
JBeaucaire Re: Loop Through all files in... 09-17-2012, 10:40 PM
dj59 Re: Loop Through all files in... 09-18-2012, 09:28 AM
JBeaucaire Re: Loop Through all files in... 09-18-2012, 12:47 PM
dj59 Re: Loop Through all files in... 09-18-2012, 04:08 PM
JBeaucaire Re: Loop Through all files in... 09-18-2012, 06:56 PM
  1. #1
    Registered User
    Join Date
    09-17-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Loop Through all files in a folder and use the same macro to edit each one.

    I am using the PERSONAL.XLSB to store my macro that I want to use on each file in a specific folder.
    It works all the way through the first file, then I get I get a runtime error 91, object variable not set. It stops at this line:
    RwExt = ActiveSheet.UsedRange.Rows.Count    'this counts the number of rows on the sheet
    Any help will be appreciated.
    Here is the entire code:

    Sub PrepData()
        Dim RwExt As Long
        Dim StrFile As String
        StrFile = Dir("C:\Access\DataLogger\1_PrepData\*.xls")
        
        Do While Len(StrFile) > 0
            Debug.Print StrFile
    
    RwExt = ActiveSheet.UsedRange.Rows.Count    'this counts the number of rows on the sheet
    
    'Add new sheet, copy data and delete old sheet
    Range("A1:G" & LTrim(Str(RwExt))).Select
    Selection.Cut
    Sheets.Add After:=Sheets(Sheets.Count)
    Columns("C:C").EntireColumn.AutoFit
    Sheets(2).Select
    Sheets(3).Paste
    Application.DisplayAlerts = False
    ActiveWindow.SelectedSheets.Delete
    Application.DisplayAlerts = True
    ActiveSheet.Name = "Data"
    
    'insert column, name, obtain user required value and fill all cells for PIN column
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight
    Range("A1") = "PIN"
    Range("A2") = InputBox("Please enter the PIN number", "PIN request")
    Range("A2:A" & LTrim(Str(RwExt))).Select
    Selection.FillDown
    
    'name EventID column
    Range("B1") = "EventID"
    
    'Name TimeStamp column
    Range("C1") = "TimeStamp"
    
    'Set user defined format for TimeStamp column
    Columns("C:C").Select
    Selection.NumberFormat = "[$-409]m/d/yy h:mm AM/PM;@"
    
    'save sheet
    ActiveWorkbook.SaveAs StrFile
    ActiveWorkbook.Close
    
            StrFile = Dir 'Loop through next file
    
        Loop
     
    End Sub

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop Through all files in a folder and use the same macro to edit each one.

    It doesn't appear the files are being explicitly opened. Something like this:

    Option Explicit
    
    Sub PrepData()
    Dim LastRw As Long, fPATH As String, fNAME As String, wbDATA As Workbook
    
        Application.DisplayAlerts = False
    
        fPATH = "C:\Access\DataLogger\1_PrepData\"
        fNAME = Dir(fPATH & "*.xls")
    
        Do While Len(fNAME) > 0
            Set wbDATA = Workbooks.Open(fPATH & fNAME)
            With wbDATA.Sheets(1)
                LastRw = .Range("A" & .Rows.Count).End(xlUp).Row
                .Range("A1:G" & LastRw).Cut
                                            'Add new sheet, copy data and delete old sheet
                wbDATA.Sheets.Add(After:=wbDATA.Sheets(wbDATA.Sheets.Count)).Name = "Data"
                Range("A1").PasteSpecial xlPasteAll
                .Delete
            End With
            'insert column, name, obtain user required value and fill all cells for PIN column
            Columns("A:C").Insert Shift:=xlToRight
            Range("A1:C1").Value = [{"PIN","EventID","TimeStamp"}]      'column headers
            Range("A2:A" & LastRw) = InputBox("Please enter the PIN number", "PIN request")
            Columns("C:C").NumberFormat = "[$-409]m/d/yy h:mm AM/PM;@"  'user defined TimeStamp format
    
            wbDATA.Close True                                           'save sheet
    
            fNAME = Dir                                                 'get next filename
        Loop
    
    End Sub
    Last edited by JBeaucaire; 09-18-2012 at 12:47 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    09-17-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Loop Through all files in a folder and use the same macro to edit each one.

    I am trying the code you posted, however now I get the error message; "runtime error 1004 method add of object sheets failed".
    In my original code I was beginning with "Option Explicit", I apologize for not having that in my posted code.
    Thanks for looking at this.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop Through all files in a folder and use the same macro to edit each one.

    My apologies, the code above is corrected.

  5. #5
    Registered User
    Join Date
    09-17-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Loop Through all files in a folder and use the same macro to edit each one.

    Excellent! Thank you.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop Through all files in a folder and use the same macro to edit each one.

    I have marked this thread solved for you.

    In the future please select Thread Tools from the menu above and mark the thread as solved. Thanks.

+ 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