VBA SetAttr function in Excel is categorized as File and Directory function. This built-in VBA SetAttr function sets the attributes of a file or directory in Excel VBA.
This function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA SetAttr Function in any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the SetAttr function, where we can use this SetAttr Function and real-time exampleS in Excel VBA.
- Overview
- Syntax of VBA SetAttr Function
- Parameters or Arguments
- Where we can apply or use VBA SetAttr Function?
- Example 1: Make a File as ReadOnly
- Example 2: Make a File as Hidden
- Example 3: Make a File as Both Hidden and ReadOnly
- Example 4: Remove File Attribute(s)
- Instructions to Run VBA Macro Code
- Other Useful Resources
Syntax of VBA SetAttr Function
The syntax of the SetAttr Function in VBA is
SetAttr(PathName,Attributes)
The SetAttr Function doesn’t return any value. The output of the VBA SetAttr function sets the attribute values of file or directory by the PathName.
Parameters or Arguments:
The SetAttr function has two arguments in Excel VBA.
where
PathName:It is a mandatory string type parameter. The PathName argument represents the path of a file, folder, or directory.
Attributes:It is a mandatory string type parameter. The Attributes argument sets the below specified values to a file. We can set combination of attribute values.
VB Constant | Value | Description |
---|---|---|
vbNormal | 0 | Normal |
vbReadOnly | 1 | Read Only |
vbHidden | 2 | Hidden |
vbSystem | 4 | System file. It’s not available on the Macintosh. |
vbArchive | 32 | File has been changed since last backup. It’s not available on the Macintosh. |
vbAlias | 64 | Supplied file name is an alias. It’s only available on the Macintosh. |
Where we can apply or use VBA SetAttr Function?
We can use this SetAttr Function in VBA MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.
Example 1: Make a File as ReadOnly
Here is a simple example of the VBA SetAttr function. This below example sets a file as read only file.
'Make a File as ReadOnly Sub VBA_SetAttr_Function_Ex1() 'Variable declaration Dim sPath As String 'Note: Specified path is an empty directory. sPath = "C:\Test\FileAttribute.xlsm" SetAttr sPath, vbReadOnly MsgBox "Set a File as ReadOnly : " & vbCrLf & sPath, vbInformation, "VBA SetAttr Function" End Sub
Output: Here is the screen shot of the first example output.
Example 2: Make a File as Hidden
Here is a simple example of the VBA SetAttr function. This below example sets a file as hidden file.
'Make a File as Hidden Sub VBA_SetAttr_Function_Ex2() 'Variable declaration Dim sPath As String 'Note: Specified path is an empty directory. sPath = "C:\Test\FileAttribute.xlsm" SetAttr sPath, vbHidden MsgBox "Set a File as Hidden: " & vbCrLf & sPath, vbInformation, "VBA SetAttr Function" End Sub
Output: Here is the screen shot of the second example output.
Example 3: Make a File as Both Hidden and ReadOnly
Here is a simple example of the VBA SetAttr function. This below example sets a file as both hidden and readonly file.
'Make a File as Both Hidden and ReadOnly Sub VBA_SetAttr_Function_Ex3() 'Variable declaration Dim sPath As String 'Note: Specified path is an empty directory. sPath = "C:\Test\FileAttribute.xlsm" SetAttr sPath, vbHidden + vbReadOnly MsgBox "Set a File as Hidden and ReadOnly: " & vbCrLf & sPath, vbInformation, "VBA SetAttr Function" End Sub
Output: Here is the screen shot of the third example output.
Example 4: Remove File Attribute(s)
Here is a simple example of the VBA SetAttr function. This below example removes all file attributes and sets as normal file.
'Remove File Attribute(s) Sub VBA_SetAttr_Function_Ex4() 'Variable declaration Dim sPath As String 'Note: Specified path is an empty directory. sPath = "C:\Test\FileAttribute.xlsm" SetAttr sPath, vbNormal MsgBox "Removed Specified File Attributes: " & vbCrLf & sPath, vbInformation, "VBA SetAttr Function" End Sub
Output: Here is the screen shot of the fourth example output.
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers