MS Excel: How to use the SETATTR Function (VBA)
This Excel tutorial explains how to use the Excel SETATTR function with syntax and examples.
Description
The Microsoft Excel SETATTR statement allows you to set the attributes of a file.
The SETATTR function is a built-in function in Excel that is categorized as a File/Directory Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.
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 set for the file. Attributes can be the following values:
VB Constant Value Explanation 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 (Macintosh only)
Returns
The SETATTR function does not return a value, but rather sets the attributes of a file.
Applies To
- Excel for Office 365, Excel 2019, 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.
Advertisements