Closing an Excel file in VBA

GeneralShamu

Board Regular
Joined
Jul 6, 2007
Messages
127
I am opening an excel file by coding up the directory and exact file name.

After I am done extracting and pasting everything into "ThisWorkbook", i.e. the workbook that has the macro to open this file, I need to close it.

I tried:
work_sheet_macro_opens.Select
ActiveWorkbook.Close

but it is not working.

Please bear in mind that given the loop structure it is not defined which workbook I will end up on (in terms of the 'active sheet/book').

Anyone have any ideas?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
How are you opening the workbook(s)?

Are you creating a reference to them when you open?

Something like this perhaps?
Code:
Set wbOpen = Workbooks.Open ("C:\NameOfWorkbookToOpen.xls")
 
' code to do stuff
' now close workbook
 
wbOpen.Close
 
Upvote 0
I define a Filepath and Filename

Then use Workbooks.Open

Then I enter a for each loop to go through every work sheet in "ActiveWorkbook.Worksheets"
 
Upvote 0
Well use something like I posted when you use Workbooks.Open.

You can use the refrerence for a lot more than just closing the workbook.
Code:
Dim wbOpen As Workbook
Dim ws As Worksheet
 
   Set wbOpen = Workbooks.Open ("C:\NameOfWorkbookToOpen.xls")
    
    For Each ws In wbOpen.Worksheets
        ' code to do stuff
    Next ws

   ' now close workbook
   wbOpen.Close
 
Upvote 0
Well use something like I posted when you use Workbooks.Open.

You can use the refrerence for a lot more than just closing the workbook.
Code:
Dim wbOpen As Workbook
Dim ws As Worksheet
 
   Set wbOpen = Workbooks.Open ("C:\NameOfWorkbookToOpen.xls")
    
    For Each ws In wbOpen.Worksheets
        ' code to do stuff
    Next ws

   ' now close workbook
   wbOpen.Close

Got it, thank you!
 
Upvote 0

Forum statistics

Threads
1,224,593
Messages
6,179,791
Members
452,942
Latest member
VijayNewtoExcel

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