Workbook Object in MS Excel VBA. Workbook is an object in Microsoft Excel represents a member of the Workbooks collection. Workbooks collection contains list of all workbook objects. Again each workbook consists of different objects like Worksheets, Ranges, cells, Charts, shapes, chart sheets, etc.
VBA Workbook Object Complete Reference Tutorial
Let us see a complete guide on VBA Workbook object. So let’s start learning about complete VBA Workbook object explained with examples in this tutorial.
Task Description | Action performed (Sample Example) |
---|---|
Workbook Reference |
|
Declare Assign Workbook variable |
‘Variable Declaration Dim oWorkbook As Workbook ‘Define Object Variable |
Create new Workbook | Workbooks.Add |
Create new Workbook with Name | ActiveWorkbbok.SaveAs Filename:=”VBA Tutorial.xlsm” |
Open Workbook | Workbooks.Open (“D:\VBA Tutorial.xlsm”) |
Open Workbook as Read Only | Workbooks.Open Filename:=”D:\VBA Tutorial.xlsm”, ReadOnly:=True |
Activate Workbook | Workbooks(“VBA Tutorial.xlsm”).Activate |
Rename Workbook | Name “D:\VBAF1\Old_File.xlsx” As “D:\VBAF1\New_File.xlsx” |
Copy Workbook | ‘Variable declaration Dim oFSO As FileSystemObject Dim sFilePath As String Dim dFilePath As String ‘Source & Destination File Path ‘Reference Workbook by Object named oWorkbook ‘Copy Workbook |
Save Workbook | Workbooks(“VBA Tutorial.xlsm”).Save |
SaveAs Workbook | ActiveWorkbook.SaveAs Filename:=”D:\VBAF1\New_File.xlsx” |
List all open Workbooks | ‘VBA List Open Workbooks in Excel Sub VBA_List_All_Open_Workbooks() ‘Variable declaration ‘Intialise value to a variable Sheets(“WB_Names”).Range(“A1”) = “Names of Available Workbooks” ‘Loop through all workbooks ‘Increase value End Sub |
List Open Unsaved Workbooks | ‘Variable declaration Dim xWorkbook As Workbook ‘Loop through all workbooks ‘Check Workbook is current Workbook or not If InStr(Right(xWorkbook.Name, 5), “.xl”) > 0 Then |
Check Workbook Exists | ‘Variable declaration Dim sFilePath As String ‘Value assigned to a variable ‘Check Workbook exists or not |
Active & Current Workbook Name | ActiveWorkbook.Name ThisWorkbook.Name |
Check for Open Workbook | ‘Variable declaration Dim oWorkbook As Workbook For Each oWorkbook In Workbooks |
Difference Between ActiveWorkbook and ThisWorkbook | ActiveWorkbook represents the Workbook in active window which has focus on the screen. ThisWorkbook represents the current Workbook in which the current VBA code is running or executing. |
Get Workbook Path & Location | ActiveWorkbook.Path ActiveWorkbook.FullName |
Protect Workbook | ActiveWorkbook.Protect Structure:=True, Windows:=False, Password:=”12345″ |
UnProtect Workbook | ActiveWorkbook.Unprotect Password:=”12345″ |
RunAutoMacros Workbook | With ActiveWorkbook .RunAutoMacros xlAutoOpen .Open End With |
Access Workbook by Index | Workbooks (1) |
Create backup of current workbook | ActiveWorkbook.SaveCopyAs “D:\VBAF1\New_File.xlsx” |
Close Workbook and Save | ActiveWorkbook.Close SaveChanges:=True |
Close Workbook without Save | ActiveWorkbook.Close SaveChanges:=False |
Delete Workbook | Kill “D:\VBAF1\Old_File.xlsx” |
Close All Workbooks without prompt | ‘Variable Declaration Dim oWorkbook As Workbook Application.ScreenUpdating = False |
Delete All Workbooks in a folder | On Error Resume Next Kill “D:\VBAF1\*.xl*” |
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers
Howdy very cool blog!! Man .. Excellent .. Superb .. I will bookmark your site and take the feeds additionallyI’m satisfied to find numerous useful information here within the put up, we need work out extra strategies on this regard, thanks for sharing. . . . . .
This is very interesting, You’re a very skilled blogger. I have joined your feed and look forward to seeking more of your excellent post.
Also, I’ve shared your site in my social networks!
We are a bunch of volunteers and starting a brand new scheme
in our community. Your website offered us with valuable info to work on. You have performed an impressive task and our whole community can be
grateful to you.