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,
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0
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 :)
 
Upvote 0

Forum statistics

Threads
1,214,525
Messages
6,120,051
Members
448,940
Latest member
mdusw

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