Running a loop until all open files are close.

Questiontoask

New Member
Joined
Aug 28, 2014
Messages
2
I am trying to write a VBA script which ends once it has changed, saved and closed all ActiveDocument / open files. The script reads like

Sub Replace ()
Do
.....

ActiveDocument.Save
ActiveDocument.Close

Until [All ActiveDocument are closed]

End Sub

I am looking for the right code for the action described in the square brackets.

Thanks in advance
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi and welcome to the forum. Try somethinng like this.

Code:
[color=darkblue]Sub[/color] Replace()
    [color=darkblue]Dim[/color] wb [color=darkblue]As[/color] Workbook
    
    [color=darkblue]For[/color] [color=darkblue]Each[/color] wb [color=darkblue]In[/color] Application.Workbooks    [color=green]'Loop through all open workbooks[/color]
        [color=darkblue]If[/color] [color=darkblue]Not[/color] wb [color=darkblue]Is[/color] ThisWorkbook [color=darkblue]Then[/color]      [color=green]'Skip the workbook that contains this macro[/color]
            
            wb.Close SaveChanges:=[color=darkblue]True[/color]      [color=green]'Save and close each workbook[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] wb
    
    [color=green]'Save and close the workbook that contains this macro[/color]
    ThisWorkbook.Close SaveChanges:=[color=darkblue]True[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,603
Members
449,089
Latest member
Motoracer88

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