Solved

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

Posted on 2014-04-15
5
205 Views
Last Modified: 2014-04-15
Experts,

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
0
Comment
Question by:morinia
  • 3
  • 2
5 Comments
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40001314
HI,

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)
    Else
        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
        Next
        ActiveWorkbook.Save
        ActiveWorkbook.Close
    End If
Next
End Sub

Open in new window

Regards
0
 

Author Comment

by:morinia
ID: 40001359
rgonzo1921,

Where do I execute this macro from?
0
 
LVL 50

Expert Comment

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

Author Comment

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

      & ObjFile.Name
0
 
LVL 50

Expert Comment

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

you only have to replace the 26 with your code
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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 tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

809 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