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

Richard1982

New Member
Joined
Jul 6, 2022
Messages
8
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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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,214,825
Messages
6,121,787
Members
449,049
Latest member
greyangel23

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