Saturday, August 13, 2016

How to use the SETATTR Function (VBA)

Description

The Microsoft Excel SETATTR statement allows you to set the attributes of a file.

Syntax

The syntax for the SETATTR function in Microsoft Excel is:
SetAttr path, attributes

Parameters or Arguments

path
The path to a file that you wish to set the attributes for.
attributes
The attributes that you'd like to attribute to the file. Attributes can be the following values:
VB ConstantValueExplanation
vbNormal0Normal (default)
vbReadOnly1Read-only
vbHidden2Hidden
vbSystem4System file
vbArchive32File has been changed since last backup
vbAlias64File name is an alias (Macintosh only)

Applies To

  • Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Function

  • VBA function (VBA)

Example (as VBA Function)

The SETATTR function can only be used in VBA code in Microsoft Excel.
Let's look at some Excel SETATTR function examples and explore how to use the SETATTR function in Excel VBA code:
SetAttr "C:\instructions.doc", vbNormal
Result: Sets the file to Normal

SetAttr "C:\instructions.doc", vbReadOnly + vbHidden
Result: Sets the file to ReadOnly and Hidden
For example:
SetAttr "c:\Test\Doc1.doc", vbReadOnly + vbHidden
In this example, the file called Doc1.doc (found in the c:\Test\ directory) would now be set to a ReadOnly, Hidden file.