VBA – Toggle Between Open Excel Files
There is often need of working in two worksheets at a time – for example when you want to loop throigh the files in a folder, and copy data from each of them into a new file, thus gathering different data into one worksheet.
Here are some small code snippets that are needed to work with multiple worksheets at a time.
'Get the name of the currently active file. You'll need this when
'toggelinig between two files, and you want to open the old file
'where the data is assembled
Dim OrginialFile
OriginalFile = Application.ActiveWorkbook.Name
'Open new file
Dim MyFile
MyFile = "C:Maria\Myfolder\Myfile.xls"
Workbooks.Open FileName:=MyFile
'Close a file
Dim MyNewFile
MyNewFile = "MyWonderfulFile.xls"
'Unable ScreenUpdating and DisoplayAlerts, so teh user isn't asked if he want tosave the changes
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Windows(MyNewFile).Close
Application.ScreenUpdating = true
Application.DisplayAlerts = true
'Toggle between open files.
Dim AnotherOpenFile
AnotherOpenFile = "MyWounderfullFile.xls"
Windows(AnotherOpenFile ).Activate
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 | |||||
Recent Posts
- VBA – Import CSV file
- VBA – Get name of file without extension
- UserForm Listbox – Populate list and extract selected items
- VBA – Retrieve Last Row From More Than One Column
- VBA – Check Extension of File
- VBA – Delete PivotTables
- VBA – Add New WorkSheet After The Last Worksheet
- VBA – Toggle Between Open Excel Files
- VBA – Looping through all files in a folder
- VBA – Create and add items to dynamic arrays
- VBA – Loop through arrays
- Excel formula – Miscellaneous
- VBA – Delete all files in a folder
- VBA – Loop through sheets
- VBA – Define PageSetup (PaperSize, Orientation, etc.) and Print Excel Sheet