code to open and close w/bs

SV18

Board Regular
Joined
Sep 1, 2008
Messages
157
Can anyone give me some code please?

I need a macro to open, save and then close all excel files from a specified location.

When it comes across a file names "Upload Controller" it needs to ignore it and continue with the task.

Can someone please help?

Regards,
 

Some videos you may like

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

btadams

Well-known Member
Joined
Jan 6, 2003
Messages
1,941
Try this (on a copy of your directory, just in case):

Code:
Sub OpenSaveCloseFiles()
Dim strPath1 As String, strFilename As String
   
   On Error GoTo OpenSaveCloseFiles_Error

    strPath1 = "C:\Data\Temp" '<- Change to your directory path

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    strFilename = Dir(strPath1 & "\*.xls")
        Do While Len(strFilename) > 0
            If strFilename <> "Upload Controller.xls" Then
                Workbooks.Open (strPath1 & "\" & strFilename)
                Application.DisplayAlerts = False
                    With ActiveWorkbook
                        .Save
                        .Close False
                    End With
                Application.DisplayAlerts = True
                strFilename = Dir
            End If
        Loop
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    MsgBox "All Done!"

   On Error GoTo 0
   Exit Sub

OpenSaveCloseFiles_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure OpenSaveCloseFiles of Module Module1"
End Sub
 

jproffer

Well-known Member
Joined
Dec 15, 2004
Messages
2,643
So you want to open each workbook...do nothing to it....save it (which seems pointless as it was already saved in the state your saving) and close it?:confused::confused:

Certainly, you can do what you want, but just out of curiousity....why would you do this?
 

SV18

Board Regular
Joined
Sep 1, 2008
Messages
157
So you want to open each workbook...do nothing to it....save it (which seems pointless as it was already saved in the state your saving) and close it?:confused::confused:

Certainly, you can do what you want, but just out of curiousity....why would you do this?

I basically have one file which has links to many others - I need to update the links by opening each file.

I've tried to use the automatic update commands but the files are so big they keep crashing and freezing Excel.

Maybe just open and close then
 

jproffer

Well-known Member
Joined
Dec 15, 2004
Messages
2,643
NO..no, no...I hadn't thought of the automatic links for a reason.

I gotcha now. And no, I'd say you would want to open, save, close just as you were.

Just curious :)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,469
Messages
5,596,312
Members
414,052
Latest member
Dual Showman

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
Top