VBA Declare Assign Workbook Object to Variable

VBA Declare Assign Workbook Object to Variable in Excel

VBA Declare Assign Workbook Object to a Variable in Excel. We can declare and assign any object to a variable. You can use Set statement to assign Workbook Object to a variable. Declared and assigned variable can be used in an entire procedure or macro.

Declare and Assign Workbook object to a Variable in Excel VBA

Let us see the following example. Here ‘oWorkbook’ is a declared Workbook object. It helps to assign Workbook Variable to an object in Excel VBA.

'Declare and Assign Workbook object to a Variable in Excel VBA
Sub VBA_declare_Assign_Workbook_Variable()
    
    'Variable declaration
    Dim oWorkbook As Workbook
    Dim sFilePath As String
    
    'Assign File Path to string variable
    sFilePath = "D:\VBAF1\VBA Functionsa.xlsm"    
   
    'Assign Object variable to a value
    Set oWorkbook = Workbooks.Open(sFilePath)
     
End Sub

In the above example ‘oWorkbook’ is an workbook object assigned to variable. And ‘sFilePath’ represents the Workbook name.

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

Leave a Reply