Looping through spreadsheets in a folder and formatting multiple tabs in each spreadsheet

Posted on 2014-04-15
Last Modified: 2014-04-15

I have a folder which contains multiple spreadsheets each with two tabs.  The spreadsheets were created in SAS and exported to Excel.  If I have a macro which formats the spreadsheets, can someone tell me how I can go through the directory in VB and format each spreadsheet?

The name of each spreadsheet begins with "Table_"
 and the two respective tabs to be formatted end with " _new"  and " _all " respectively
Question by:morinia
  • 3
  • 2
LVL 49

Accepted Solution

Rgonzo1971 earned 500 total points
ID: 40001314

pls try

Sub macro3()
Set fso = CreateObject("Scripting.FileSystemObject")
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
    .Title = "Select the folder"
    If .Show = True Then
        FolderName = .SelectedItems(1)
        MsgBox "No Folder selected!", vbOKOnly, "File Export"
        Exit Sub
    End If
End With
Set ObjFolder = fso.GetFolder(FolderName)
On Error GoTo 0
If IsEmpty(ObjFolder) Then
    MsgBox "Folder not valid"
    Exit Sub
End If

Set ObjFiles = ObjFolder.Files
For Each ObjFile In ObjFiles
    If ObjFile.Name Like "Table_*.xls*" Then
        Workbooks.Open (FolderName & "\" & ObjFile.Name)
        For Each sh In ActiveWorkbook.Sheets
            If sh.Name Like "*_new" Or sh.Name Like "*_all" Then
                ' YourCode
            End If
    End If
End Sub

Open in new window


Author Comment

ID: 40001359

Where do I execute this macro from?
LVL 49

Expert Comment

ID: 40001366
At best from another excel workbook not one you are trying to change

Author Comment

ID: 40001392
Pardon my ignorance, but I know what to input for Folder Name.  Can you tell me what is

      & ObjFile.Name
LVL 49

Expert Comment

ID: 40001402
the code will present you with a folder selector dialog

you only have to replace the 26 with your code

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now