Excel/VBA automatically opening file - can I avoid this?

Richard1982

New Member
Joined
Jul 6, 2022
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I have a small section of VBA code (as part of a larger macro) which takes some of the pages of the report I'm working on and saves them to a new file.

VBA Code:
Dim wb As Workbook

Set wb = Workbooks.Add
   ThisWorkbook.Worksheets(Array("Page 1", "Page 3", "Page 4", "Page 6", "Page 7")).Copy Before:=wb.Sheets(1)
   
'This bit deletes Sheet 1 which is there by default
   wb.Sheets("Sheet1").Delete
            
   wb.SaveAs "Z:\Reporting\Report.xlsx"

This copies the pages I want, inserts them in after 'sheet1' (By default my excel starts all workbooks with just one sheet), then deletes 'sheet1'. It then saves the file to the location of my choice. As is it works absolutely fine, no problems. The only thing that I'd like to improve is that the file is opened up on the screen and I need to close it down. Is there any way to avoid this and just have it save the file without showing me?

I'm still learning so I apologise if this code isn't the most elegant of solutions! Any help would be greatly appreciated :)

Richard
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
add the line:
VBA Code:
wb.Close False
Under the saveas line
 
Upvote 0
Solution
That's perfect, thank you.
The file creates many different files and this saves me closing them all afterwards.
 
Upvote 0
Hi Richard,

or maybe try

VBA Code:
  ThisWorkbook.Worksheets(Array("Page 1", "Page 3", "Page 4", "Page 6", "Page 7")).Copy
            
   ActiveWorkbook.SaveAs "Z:\Reporting\Report.xlsx"

Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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