VBA GetAttr function in Excel is categorized as File and Directory function. This built-in VBA GetAttr statement returns the attributes of a file, folder, 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 GetAttr 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 GetAttr function, where we can use this GetAttr Function and real-time examples in Excel VBA.
- Overview
- Syntax of VBA GetAttr Function
- Parameters or Arguments
- Where we can apply or use VBA GetAttr Function?
- Example 1: Find the file attribute(Archive)
- Example 2: Find the file attribute(Directory)
- Example 3: Find the File Attribute Value(Returns an Error)
- Example 4: Find the File Attribute Directory Value
- Instructions to Run VBA Macro Code
- Other Useful Resources
Syntax of VBA GetAttr Function
The syntax of the GetAttr Function in VBA is
GetAttr(PathName)
The GetAttr Function returns an integer value. The output of the VBA GetAttr function can be a combination of several attributes. Here are the different list of the GetAttr function 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. |
vbDirectory | 16 | Directory or Folder |
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. |
Parameters or Arguments:
The GetAttr function has one argument in Excel VBA.
where
PathName:It is a mandatory string type parameter. The PathName argument represents the path of a file, folder, or directory. It helps to retrieve the details of file, folder, or directory.
Where we can apply or use VBA GetAttr Function?
We can use this GetAttr 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: Find the file attribute(Archive)
Here is a simple example of the VBA GetAttr function. This below example finds the file attribute value. And it returns output as 32.
'Find the file attribute(Archive) Sub VBA_GetAttr_Function_Ex1() 'Variable declaration Dim sPath As String Dim iOutput As Integer sPath = "C:\Someswari\VBAF1\VBA Functions\VBA Text Functions\VBA Functionsa.xlsm" iOutput = GetAttr(sPath) MsgBox "File Attribute Value is : " & iOutput, vbInformation, "VBA GetAttr Function" End Sub
Output: Here is the screen shot of the first example output.
Example 2: Find the file attribute(Directory)
Here is a simple example of the VBA GetAttr function. This below example finds the file attribute value. And it returns output as 16.
'Find the file attribute(Directory) Sub VBA_GetAttr_Function_Ex2() 'Variable declaration Dim sPath As String Dim iOutput As Integer sPath = "C:\Someswari\VBAF1\VBA Functions\VBA Text Functions\" iOutput = GetAttr(sPath) MsgBox "File Attribute Value is : " & iOutput, vbInformation, "VBA GetAttr Function" End Sub
Output: Here is the screen shot of the second example output.
Example 3: Find the File Attribute Value(Returns an Error)
Here is a simple example of the VBA GetAttr function. This below example finds for the file attribute value.The below macro returns an error. Because the specified file is not available.
'Find the File Attribute Value(Returns an Error) Sub VBA_GetAttr_Function_Ex3() 'Variable declaration Dim sPath As String Dim iOutput As Integer sPath = "C:\VBAF1\VBA Functions\VBA Text Functions\VBA Functionsa.xlsm" iOutput = GetAttr(sPath) MsgBox "File Attribute Value is : " & iOutput, vbInformation, "VBA GetAttr Function" End Sub
Output: Here is the screen shot of the third example output.
Example 4: Find the File Attribute Directory Value
Here is a simple example of the VBA GetAttr function. This below example checks for the file attribute and returns value as 16.
'Find the File Attribute Directory Value Sub VBA_GetAttr_Function_Ex4() 'Variable declaration Dim sPath As String Dim iOutput As Integer Dim iDirectory As Integer sPath = "C:\Someswari\VBAF1\VBA Functions\VBA Text Functions" iOutput = GetAttr(sPath) iDirectory = iOutput And vbDirectory MsgBox "File Attribute Directory Value is : " & iDirectory, vbInformation, "VBA GetAttr 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