VBA SetAttr function

SetAttr Function Description

The VBA SetAttr function sets the attributes of a file or directory. The SetAttr function is especially useful when wanting to hide files or make them readonly.

Syntax

The syntax for the SetAttr function in VBA is:

SetAttr( path, attributes )

Parameters

path
The path to a file or directory for which you want to change attributes. The following attributes are available for this function:

Constant Value Description
vbNormal 0 Normal (default)
vbReadOnly 1 Read-only
vbHidden 2 Hidden
vbSystem 4 System file
vbArchive 32 File has been changed since last backup
vbAlias 64 File name is an alias (Mac only)

Other Notes

The attribute can be a single value or a combination of multiple values. To combine values use the And operator or simply add the values.

Example usage

The SetAttr function can only be used in VBA code. Let’s look at some VBA SetAttr function examples:

SetAttr "C:\test.txt", vbHidden
'Result: File "test.txt" is now a Hidden file

SetAttr "C:\test.txt", vbReadOnly And vbHidden
'Result: File "test.txt" is now a ReadOnly and a Hidden file