Excel tutorial use vba to open ※ Download: http://vepenragi.tiogalo.ru/?dl&keyword=excel+tutorial+use+vba+to+open&source=bitbin.it2 We do this by changing the code name of the worksheet. Or, if you have department wise reports and you want to split these into individual workbooks so that you can send these individual workbooks to the department heads. For example, what happens if you read the number 99. Open property to open up an excel file already present in out E Drive. We have already seen the basics of the Workbooks. The resulting Open dialog box looks as follows. I would also like some guidence as to what i can do to make the loop end when the cells are empty. This is the range H3:H30. So if you use a VBA code to add a new workbook, the ActiveWorkbook would change, but ThisWorkbook would still refer to the one that has the glad. However these are slow, cumbersome and unnecessary. It is better practice to give these sheets more meaningful names. Using Index Numbers You can also refer to the workbooks based on their index number. End of Activity 2 You can have as many modules as you like in a north and as many subs as you like within a module. However, I include it for purposes of specifying file filtering criteria. Pressing Shift and Tab will tab to the left. Using Workbook Object in Excel VBA (Open, Close, Save, Set) - In VBA we can take any item before a full stop and use the With statement on it. In this tutorial, I will cover the how to work with workbooks in Excel using VBA. Within a workbook, you have different objects such as , chart sheets, , chart objects, shapes, etc. With VBA, you can do a lot of stuff with a workbook object — such as open a specific workbook, save and close workbooks, create new workbooks, change the workbook properties, etc. The method you choose would depend on what you want to get done. In this section, I will cover the different ways to refer to a workbook along with some example codes. Using Workbook Names If you have the exact name of the workbook that you want to refer to, you can use the name in the code. If you have two workbooks open, and you want to activate the workbook with the name — Examples. Activate End Sub Note that you need to use the file name along with the extension if the file has been saved. If you want to activate a workbook and select a specific cell in a worksheet in that workbook, you need to give the entire address of the cell including the Workbook and the Worksheet name. Select End Sub The above code first activates Sheet1 in the Examples. However, in some cases, you do need to specify the workbook to make sure the code works more on this in the ThisWorkbook section. Using Index Numbers You can also refer to the workbooks based on their index number. For example, if you have three workbooks open, the following code would show you the names of the three workbooks in a message box one at a time. Sub WorkbookName MsgBox Workbooks 1. Name MsgBox Workbooks 2. Name MsgBox Workbooks 3. One of the troubles I often have with using index numbers with Workbooks is that you never know which one is the first workbook and which one is the second and so on. To be sure, you would have to run a code as shown above or something similar to loop through the open workbooks and know their index number. Excel treats the workbook opened first to have the index number as 1, and the next one as 2 and so on. Despite this drawback, using index numbers can come in handy. For example, if you want to loop through all the open workbooks and save all, you can use the index numbers. The below code would loop through all the open workbooks and close all except the workbook that has this VBA code. Name Then Workbooks i. Close End If Next i End Sub The above code counts the number of open workbooks and then goes through all the workbooks using the. It uses the to check if the name of the workbook is the same as that of the workbook where the code is being run. Note that we have run the loop from WbCount to 1 with a Step of -1. This is done as with each loop, the number of open workbooks is decreasing. ThisWorkbook is covered in detail in the later section. Using ActiveWorkbook ActiveWorkbook, as the name suggests, refers to the workbook that is active. The below code would show you the name of the active workbook. Sub ActiveWorkbookName MsgBox ActiveWorkbook. Name End Sub When you use VBA to activate another workbook, the ActiveWorkbook part in the VBA after that would start referring to the activated workbook. Here is an example of this. If you have a workbook active and you insert the following code into it and run it, it would first show the name of the workbook that has the code and then the name of Examples. Sub ActiveWorkbookName MsgBox ActiveWorkbook. Name End Sub Note that when you create a new workbook using VBA, that newly created workbook automatically becomes the active workbook. Using ThisWorkbook ThisWorkbook refers to the workbook where the code is being executed. Every workbook would have a ThisWorkbook object as a part of it visible in the Project Explorer. An is something that is triggered based on an event — such as double-clicking on a cell, or saving a workbook or activating a worksheet. For example, if you double-click on the ThisWorkbook object in the Project Explorer and copy paste the below code in it, it will show the cell address whenever you double-click on any of the cells in the entire workbook. The below code would return the name of the workbook in which the code is being executed. Sub ThisWorkbookName MsgBox ThisWorkbook. Name End Sub The benefit of using ThisWorkbook over ActiveWorkbook is that it would refer to the same workbook the one that has the code in it in all the cases. So if you use a VBA code to add a new workbook, the ActiveWorkbook would change, but ThisWorkbook would still refer to the one that has the code. Creating a New Workbook Object The following code will create a new workbook. Add End Sub When you add a new workbook, it becomes the active workbook. The following code will add a new workbook and then show you the name of that workbook which would be the default Book1 type name. Name End Sub Open a Workbook using VBA You can use VBA to open a specific workbook when you know the file path of the workbook. The below code will open the workbook — Examples. To avoid this error, you can add a few lines to your code to first check whether the file exists or not and if it exists then try to open it. Open FilePath End Sub The above code opens the Open dialog box. When you select a file that you want to open, it assigns the file path to the FilePath variable. Open then uses the file path to open the file. Saving a Workbook To save the active workbook, use the code below: Sub SaveWorkbook ActiveWorkbook. Save End Sub This code works for the workbooks that have already been saved earlier. The below code would save the active workbook as a. The below code saves the file on my desktop. The below code shows the Saveas dialog box and allows the user to select the location where the file should be saved. Saving all Open Workbooks If you have more than one workbook open and you want to save all the workbooks, you can use the code below: Sub SaveAllWorkbooks Dim wb As Workbook For Each wb In Workbooks wb. Save Next wb End Sub The above saves all the workbooks, including the ones that have never been saved. The workbooks that have not been saved previously would get saved in the default location. If you only want to save those workbooks that have previously been saved, you can use the below code: Sub SaveAllWorkbooks Dim wb As Workbook For Each wb In Workbooks If wb. Save End If Next wb End Sub Saving and Closing All Workbooks If you want to close all the workbooks, except the workbook that has the current code in it, you can use the code below: Sub CloseandSaveWorkbooks Dim wb As Workbook For Each wb In Workbooks If wb. In case there are changes in these workbooks, the changes would be saved. In case there is a workbook that has never been saved, it will show the save as dialog box. Save a Copy of the Workbook with Timestamp When I am working with complex data and dashboard in Excel workbooks, I often create different versions of my workbooks. This is helpful in case something goes wrong with my current workbook. I would at least have a copy of it saved with a different name and I would only lose the work I did after creating a copy. Here is the VBA code that will create a copy of your workbook and save it in the specified location. While this works great, I would feel more comfortable if I had different copies saved whenever I run this code. The reason this is important is that if I make an inadvertent mistake and run this macro, it will save the work with the mistakes. To handle such situations, you can use the below code that saves a new copy of the work each time you save it. And it also adds a date and timestamp as a part of the workbook name. This can help you track any mistake you did as you never lose any of the previously created backups. Create a New Workbook for Each Worksheet In some cases, you may have a workbook which has multiple worksheets, and you want to create a workbook for each worksheet. Or, if you have department wise reports and you want to split these into individual workbooks so that you can send these individual workbooks to the department heads. Here is the code that will create a workbook for each worksheet, give it the same name as that of the worksheet, and save it in the specified folder. Sub CreateWorkbookforWorksheets Dim ws As Worksheet Dim wb As Workbook For Each ws In ThisWorkbook. The code goes through each worksheet using the and creates a workbook for it. It also uses the copy method of the worksheet object to create a copy of the worksheet in the new workbook. You can use this technique to assign a workbook object to a variable. This is covered in the next section. Assign Workbook Object to a Variable In VBA, you can assign an object to a variable, and then use the variable to refer to that object. For example, in the below code, I use VBA to add a new workbook and then assign that workbook to the variable wb. To do this, I need to use the SET statement. Once I have assigned the workbook to the variable, all the properties of the workbook are made available to the variable as well. This tells VBA that this variable can hold the workbook object. The next statement uses SET to assign the variable to the new workbook that we are adding. Once this assignment is done, we can use the wb variable to save the workbook or do anything else with it. Looping through Open Workbooks We have already seen a few examples codes above that used looping in the code. In this section, I will explain different ways to loop through open workbooks using VBA. Suppose you want to save and close all the open workbooks, except the one with the code in it, then you can use the below code: Sub CloseandSaveWorkbooks Dim wb As Workbook For Each wb In Workbooks If wb. Name Then Workbooks i. When we have the count, we use the For Next loop to go through each workbook. Also, we use the index number to refer to the workbooks in this case. Note that in the above code, we are looping from WbCount to 1 with Step -1. This is needed as with each loop, the workbook gets closed and the number of workbooks gets decreased by 1. Generally, VBA errors are not very informative and often leave it to you to figure out what went wrong. For example, if I am trying to access the fifth workbook using Workbooks 5 , and there are only 4 workbooks open, then I will get this error. For example, if your workbook name is Examples. For example, if your workbook name is Book1, and you use the name Book1. Offset i - 1, 0. Name Next i End Sub The above code adds a new worksheet and then lists the name of all the open workbooks. Offset i - 1, 0. Name Next i End Sub Open the Specified Workbook by Double-clicking on the Cell If you have a list of file paths for Excel workbooks, you can use the below code to simply double-click on the cell with the file path and it will open that workbook. Value End Sub This code would be placed in the ThisWorkbook code window. Note that the ThisWorkbook object should be in the workbook where you want this functionality. Now, if you have the exact path of the files that you want to open, you can do that by simply double-clicking on the file path and VBA would instantly open that workbook. Where to Put the VBA Code Wondering where the VBA code goes in your Excel workbook? Excel has a VBA backend called the VBA editor. You need to copy and paste the code into the VB Editor module code window. This will open the VB editor in the backend. This will insert a module object for your workbook.