[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


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

Posted on 2014-04-15
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 53

Accepted Solution

Rgonzo1971 earned 2000 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 53

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 53

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

649 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