How to use the property of variable type "Workbook" Excel (Excel) Macro (VBA)

Excel Macro (VBA)
The article contains advertisements.

The use of the variable type "Workbook" is described in this blog.

How to use the variable type "Workbook" Excel(Excel) Macro(VBA)

Here is a more detailed explanation of the "Workbook" property.

Workbook Properties

Workbook properties are various types of information about the book. Using these properties, you can examine and include information about the book in question.

Here are some typical frequently used properties.

Property Nametreatment
nameBook Name
PathBook save path
FullNameBook save path + Book name
ReadOnlyWhether this book is read-only or not
SavedWhether it has been saved since it was last edited

We will look at how to use it specifically.

.name

Sub Workbook Usage()
    Dim tb As Workbook
    Set tb = ThisWorkbook
    MsgBox tb.name
End Sub

When I run this code

was displayed.

Dim tb As Workbook" to declare that "tb" is a workbook, and

With "Set tb = ThisWorkbook", "tb" contains the book in which the macro is being executed.

MsgBox tb.name" displays the file name of workbook "tb" in a message box.

.Path

Change the .name part to .Path.

Sub Workbook Usage()
    Dim tb As Workbook
    Set tb = ThisWorkbook
    MsgBox tb.Path
End Sub

When I run this code

was displayed.

The folder where this book is stored is displayed.

.FullName

Change the .name part to .FullName.

Sub Workbook Usage()
    Dim tb As Workbook
    Set tb = ThisWorkbook
    MsgBox tb.FullName
End Sub

When I run this code

was displayed.

The folder name to the file name where this book is stored is displayed.

.ReadOnly

Change the .name part to .ReadOnly.

Sub Workbook Usage()
    Dim tb As Workbook
    Set tb = ThisWorkbook
    MsgBox tb.ReadOnly
End Sub

When I run this code

was displayed.

False" was returned because this book is not read-only.

I changed it to read-only to try it out and ran it.

True" was returned because it is now read-only.

.Saved

Change the .name part to .Saved.

Sub Workbook Usage()
    Dim tb As Workbook
    Set tb = ThisWorkbook
    MsgBox tb.Saved
End Sub

When I run this code

was displayed.

This means that the file has been saved since it was last edited.

Now edit the cell contents slightly and run it again.

was displayed.

This means that you have not yet saved the file after the last edit.

There are many other properties in addition to these, but I think it is sufficient to keep this area in mind for now.

Comment

Copied title and URL