Opening the workbooks in a folder, one by one, to make chgs

EdinVA

Board Regular
Joined
Feb 11, 2003
Messages
70
I'd like to create a macro which will

-go to a given folder
-open each of the workbooks in the folder, one at a time

-after opening the first wkbk, go to a specific cell address on the first tab of the wkbk
-paste in some values and formulas
-save the workbook
-open the next workbook, and repeat as above, for all workbooks in the folder

Any help would be appreciated.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
One quick sample:
Code:
Sub asdf()
Dim Bk As Variant

'the folder you want to search
myDir = "C:\ThisFolder"

'search folder for Excel (.xls) files
Bk = Dir(myDir & "\*.xls")
'if blank, no Excel files found in directory--end macro here
If Bk = "" Then
    MsgBox "No Excel files found in folder."
    Exit Sub
End If

'loop through all .xls files found in folder
Do While Bk <> ""
    If Bk <> ThisWorkbook.Name Then
        'open the workbook
        Set Bk = Workbooks.Open(Filename:=myDir & Bk)
            
            '**perform your actions on the workbook here**
            
            Application.DisplayAlerts = False
            With Bk
                .Save 'save opened workbook
                .Close 'close opened workbook
            End With
            Application.DisplayAlerts = True
        End If
    End If
    Bk = Dir()
Loop 'loop to next workbook

End Sub
 
Upvote 0
Mine is different in that I use the Filesearch Object so that if I wanted to -for example: search on files modified by a certain person or before a certain date -I can.

I also used someone's code (from here) to breakdown the filename.

Code:
Sub Open_All_XLS_in_Folder()

Application.ScreenUpdating = False

 Dim DataFileLocation As String 'folder location
 Dim Workbookname As String
 Dim XLS_filename(78) As String 'max right now is 78 change to taste
 Dim Filename_Convention As String
 Dim New_Workbook As Workbook
 
 Filename_Convention = "*.xls"
 
 DataFileLocation = "C:\" 'must end in "\"
 
 
With Application.FileSearch
    .NewSearch
    .LookIn = DataFileLocation
    .Filename = "*.xls"
    .SearchSubFolders = False
    .FileType = msoFileTypeAllFiles
        
    If .Execute > 0 Then
       num_files = .FoundFiles.Count

        'the following for statement
        For i = 1 To num_files
            XLS_filename(i) = GetShortName(.FoundFiles(i))
        Next i
    End If
       
End With
 
If num_files = 0 Then
    MsgBox ("No XLS files found")
    Error (665)
End If
 





For j = 1 To num_files Step 1

    Workbooks.Open DataFileLocation & XLS_filename(j)
    Set New_Workbook = Workbooks(XLS_filename(j))


    'put your stuff here

    New_Workbook.SaveAs DataFileLocation & Replace(XLS_filename(j), ".xls", ".2.xls")
    New_Workbook.Close


Next j

Application.ScreenUpdating = True
End Sub
Function FileNamePosition(sFullName As String) As Integer
Dim bFound As Boolean
Dim nPosition As Integer

bFound = False
nPosition = Len(sFullName)

Do While bFound = False
    ' Make sure we were not dealt a
    ' zero-length string
    If nPosition = 0 Then Exit Do
        
    'End If

    ' We are looking for the first "\"
    ' from the right.
    If Mid(sFullName, nPosition, 1) = "\" Then
        bFound = True
    Else
' Working right to left
    nPosition = nPosition - 1
End If



Loop

If bFound = False Then
    FileNamePosition = 0
Else
    FileNamePosition = nPosition
End If

End Function

Function GetShortName(sLongName As String) As String
Dim sPath As String
Dim sShortName As String

BreakdownName sLongName, sShortName, sPath

GetShortName = sShortName

End Function

Sub BreakdownName(sFullName As String, _
                    ByRef sName As String, _
                    ByRef sPath As String)
                    
    Dim nPos As Integer
    
    'Find out where the filename begins
    nPos = FileNamePosition(sFullName)
    
    If nPos > 0 Then
        sName = Right(sFullName, Len(sFullName) - nPos)
        sPath = Left(sFullName, nPos - 1)
    Else
        'invalid sFullname -don't change anything
    End If
    
End Sub
 
Upvote 0
Mine is different in that I use the Filesearch Object so that if I wanted to -for example: search on files modified by a certain person or before a certain date -I can.
That's all well and good, but keep in mind that FileSearch was removed from Excel 2007.

I originally used FileSearch in the project I took the code I posted from, but I went ahead and redid it using Dir just in case they ever upgrade. (Plus, it gave me something to do for a few days. ;) )
 
Upvote 0
Thanks very much for your responses. I've been ill and haven't been able to try this yet. I had asked one of my staff members with VB code writing experience to try it for me, but he apparently wants to point out all the reasons it won't work, etc.

I've recorded macros, but have no experience writing them. Suggestions on learning?
 
Upvote 0
Thanks very much for your responses. I've been ill and haven't been able to try this yet. I had asked one of my staff members with VB code writing experience to try it for me, but he apparently wants to point out all the reasons it won't work, etc.

I've recorded macros, but have no experience writing them. Suggestions on learning?

I would start here:

http://www.mrexcel.com/sunshop/index.php?action=category&id=3
 
Upvote 0
I set up a 3 workbook folder to test the macro below:

Code:
Sub asdf()
Dim Bk As Variant

'the folder you want to search
myDir = "C:\My Documents\PP Test"

'search folder for Excel (.xls) files
Bk = Dir(myDir & "\*.xls")
'if blank, no Excel files found in directory--end macro here
If Bk = "" Then
    MsgBox "No Excel files found in folder."
    Exit Sub
End If

'loop through all .xls files found in folder
Do While Bk <> ""
    If Bk <> ThisWorkbook.Name Then
        'open the workbook
        Set Bk = Workbooks.Open(Filename:=myDir & Bk)
            
            '**perform your actions on the workbook here**
            
            Application.DisplayAlerts = False
            With Bk
                .Save 'save opened workbook
                .Close 'close opened workbook
            End With
            Application.DisplayAlerts = True
        End If
    End If
    Bk = Dir()
Loop 'loop to next workbook

End Sub

When I tried stepping into the macro I got an error saying End If without Block End If, and the first End If was highlighted. I removed the first End If and when I tried to step through again, got back the message "No Excel files found in folder."

What did I do wrong?

[Added code tags~VP]
 
Upvote 0
I did delete one, and that's when I got the message "No Excel files found in folder".
 
Upvote 0
Do you have the correct directory? For example, most Windows installs list the "My Documents" folder under C:\Documents and Settings\..., not just C:

Also, spelling counts. Make sure there are no typos in the directory path.
 
Upvote 0

Forum statistics

Threads
1,216,000
Messages
6,128,202
Members
449,433
Latest member
mwegter95

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