VBA to run through multiple files in a folder and perform an action

amitojsd

New Member
Joined
Jan 16, 2025
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi everyone, I am trying to write a VBA code that runs through a directory of excel files of the same format, that unhides and unlinks all the sheets.
I was able to make the unhide and unlink portion work, but running it through the directory is where I am getting stuck.

I open the first file in the directory, try to run the macro and excel shuts down. I have 90p files in the directory, and each file is about 2MB

Here is the code I have written till now -


Sub DirectoryFileLoopUnhideAndUnlink()

Dim fileDirectory As String

Dim fileCriteria As String

Dim fileName As String

Dim fileToOpen As Workbook


Application.ScreenUpdating = False


fileDirectory = "C:\Users\Downloads\TEST Loop\"

fileName = Dir(fileDirectory)

Do While Len(fileName) > 0


For Each Sheet In Sheets

Sheet.Visible = True

Next Sheet

Sheets("ABBrand").Select

Range("D17").Select

ActiveSheet.ListObjects("Brand").Unlink

Sheets("ABItem").Select

Range("J11").Select

ActiveSheet.ListObjects("Item").Unlink


Loop

Application.ScreenUpdating = True

End Sub


Can you please let me know what I am doing wrong?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Untested here. Let me know if it works for you.

VBA Code:
Sub DirectoryFileLoopUnhideAndUnlink()

    Dim fileDirectory As String
    Dim fileName As String
    Dim fileToOpen As Workbook
    Dim ws As Worksheet
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    ' Define the directory path
    fileDirectory = "C:\Users\Downloads\TEST Loop\"
    fileName = Dir(fileDirectory & "*.*") ' Adjusted to pick up any file type
    
    ' Loop through all files in the directory
    Do While fileName <> ""
        ' Open the workbook
        On Error Resume Next
        Set fileToOpen = Workbooks.Open(fileDirectory & fileName)
        On Error GoTo 0

        If Not fileToOpen Is Nothing Then
            ' Unhide all sheets
            For Each ws In fileToOpen.Sheets
                ws.Visible = True
            Next ws

            ' Perform actions on specific sheets
            On Error Resume Next
            With fileToOpen
                .Sheets("ABBrand").Activate
                .Sheets("ABBrand").ListObjects("Brand").Unlink
                
                .Sheets("ABItem").Activate
                .Sheets("ABItem").ListObjects("Item").Unlink
            End With
            On Error GoTo 0
            
            ' Save and close the workbook
            fileToOpen.Save
            fileToOpen.Close
        End If
        
        ' Move to the next file
        fileName = Dir
    Loop

    ' Restore settings
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

    MsgBox "All files have been processed.", vbInformation
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,225,738
Messages
6,186,734
Members
453,369
Latest member
juliewar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top