Excel VBA Workbook Object Methods
Activate
Close
Protect
ProtectSharing
RefreshAll
RejectAllChanges
RemoveUser
Save
SaveAs
SaveCopyAs
UnProtect
UnProtectSharing
Method :- Active Example
Workbooks("Project1").Activate
Workbooks(2).Activate
Workbooks("Book3").Activate
Method :- Close Example
ActiveWorkbook.Close
Example 2:-
Workbooks (“ D:\Sample.xlsx").Close Savechanges:=False
Method :- Protect Example
ActiveWorkbook.Protect Password:="YourPassword", Structure:=True, Windows:=True
Method :- Protect Share Example
ActiveWorkbook.protectSharing Password:=”YourPassword”, SharingPassword:=”SharedPassword”
Method :- Refresh All Example
Workbooks(“Your Workbook Name”).RefreshAll
VBA RefreshAll Workbook Method-2
ActiveWorkbook.RefreshAll
VBA RefreshAll Workbook Method-3
Workbooks("Sample.xls").RefreshAll
It will refreshes the second workbook.
Workbooks(2).RefreshAll
-----------------------------------------------------------------------------
VBA RemoveUser Workbook:Example 1
Sub Remove_User1()
Dim UsrList()
UsrList = ThisWorkbook.UserStatus
If UBound(UsrList) > 1 Then
ThisWorkbook.RemoveUser (2)
End If
End Sub
----------------------------------------------------------------------------------
VBA Save Workbook: Example 1
Active Workbook.Save
VBA Save Workbook: Example 2
Sub Save_Workbook()
Dim Wkb As Workbook
Set Wkb = Workbooks.Add
Wkb.Save
End Sub
----------------------------------------------------------
VBA SaveAS Workbook Example 1
Sub SaveAs_Workbook()
Dim Wkb As Workbook
Set Wkb = Workbooks.Add
ActiveWorkbook.SaveAs Filename:="C:\Sample.xlsm"
End Sub
-----------------------------------------------------------------
VBA SaveCopyAS Workbook:Example 1
Sub Workbook_SaveCopyAs()
ThisWorkbook.SaveCopyAs ThisWorkbook.Name & "_Ver1"
End Sub
-----------------------------------------------------------------------
VBA Unprotect Workbook: Example 1
Sub Unprotect_Sheet()
ActiveWorkbook.Unprotect “YourPassword”, True, True
End Sub
------------------------------------------------------------------
VBA UnProtectSharing Workbook:Example 1
Sub Unprotect_Sheet()
ActiveWorkbook.UnprotectSharing “YourPassword”
End Sub
--------------------------------------------------------
VBA RemoveUser Workbook:Example 2
Sub Remove_User2()
Dim UsrList()
UsrList = ThisWorkbook.UserStatus
For i = 1 To UBound(UsrList)
ThisWorkbook.RemoveUser (i)
Next
End Sub
---------------------------------------------------------