Solved

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

Posted on 2014-04-15
5
204 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 49

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 49

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 49

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

776 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