Working with workbooks objects

Adding a workbooks



Sub workbook_object_demo()

Workbooks.Add

End Sub



Counting total workbooks



Sub workbook_object_demo()

c = Workbooks.Count

Debug.Print c

End Sub


Opening workbooks



Sub workbook_object_demo()

' this will open the excel file with the name filename.xlsm

Workbooks.Open ("filename.xlsm")
  
End Sub



Activating specific workbook



Sub workbook_object_demo()
  
' this will activate the excel file with the name filename.xlsm
Workbooks("filename.xlsm").Activate

End Sub



Closing specific workbook



Sub workbook_object_demo()

' this will close the excel file with the name filename.xlsm
Workbooks.Close

End Sub



Looping through workbooks


Sub workbook_object_demo()

Dim book As Workbook

For Each book In Workbooks

Debug.Print ThisWorkbook.FullName

Next

End Sub


To loop thorough all workbooks we require workbook data type variable. we have declared a variable named book , which is a workbook data type, because we want to loop through all the workbook we have created a for each loop, which will loop through all the workbooks and print the workbook names

Post a Comment

0 Comments