Closing Multiple Workbooks using MACRO

nassr

New Member
Joined
Sep 8, 2006
Messages
11
Need to create a Macro that goes to a workbook and a specific worksheet and copies a defined range of cells. It then returns to a centalized page where the data is pasted. This process continues for multiple workbooks until the loop is complete (The Directory Names and File Names for the workbooks are defined in a worksheet).

My problem is that I cannot find a way to close the workbooks that the initial data was copied from once that process has taken place. I cannot call out the actual name because there will be multiple ones used. I do not know how to make it work using the variable name I assigned (MyFile) either?

Any suggestions are appreciated. The code I used is as follows:


Sub CreateGroupProcurementsFile()
'
Application.ScreenUpdating = False

'create variable for user path and file
Dim MyPath
Dim MyFile

'start at first file
MACRO.Select
Range("A7").Select

'create loop for each file
Do Until ActiveCell = ""
Let MyPath = ActiveCell.Value
ActiveCell.Offset(0, 1).Range("A1").Select
Let MyFile = ActiveCell.Value
ChDir MyPath
Workbooks.Open Filename:=MyFile, UpdateLinks:=0

'Copy and Paste Range file

Sheets("Plan").Select
Range("A51:T1200").Copy

Windows("GroupProcurements.xls").Activate
GroupProcurement.Select
Range("A65536").End(xlUp).Select
ActiveCell.Offset(1, 0).Select

Selection.PasteSpecial Paste:=xlPasteValues

'move down to next file
MACRO.Select
ActiveCell.Offset(0, -1).Range("A1").Select
ActiveCell.Offset(1, 0).Range("A1").Select
Loop
'end of loop above

GroupProcurement.Select

End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,353
Office Version
  1. 365
Platform
  1. Windows
What you probably want to do is create a reference to the workbook you've opened.

Something like this perhaps.
Code:
Set wbOpen =  Workbooks.Open (Filename:=MyFile)
You can then use this to close the workbook.
Code:
wbOpen.Close
 

Forum statistics

Threads
1,176,287
Messages
5,902,328
Members
434,962
Latest member
sgilmoreBBP

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
Top