VBA FileAttr Function

VBA FileAttr Function in Excel

VBA FileAttr function in Excel is categorized as a File and Directory function. This built-in VBA FileAttr function returns the file mode for opened files using the open statement in Excel VBA. It returns an integer value.

This function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA FileAttr 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 FileAttr function, where we can use this FileAttr Function and real-time exampleS in Excel VBA.

Syntax of VBA FileAttr Function

The syntax of the FileAttr Function in VBA is

FileAttr(FileNumber,[ReturnType])

The FileAttr Function returns an integer value.

Parameters or Arguments:

The FileAttr function has two arguments in Excel VBA.
where
FileNumber: It is mandatory parameter. The FileNumber argument represents a number of the file. It contains an integer value. If specified file number doesn’t exist, returns an error.
ReturnType: It is an optional parameter. The Attribute represents the type of information to return. It contains an integer value either 1 or 2. If you omit the argument, the default value of the return type is 1.

Where
1 – It returns the mode of the file.( Applicable in 32 bit systems)
2 – It returns the operating system file handler.( Applicable in 16 bit systems)

Note: If return type is specified as 1, the VBA FileAttr function returns the mode of the specified open file. These values are shown in the following table.

Value Mode
1 Input
2 Output
4 Random
8 Append
32 Binary

Where we can apply or use VBA FileAttr Function?

We can use this FileAttr 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: ‘Check the mode of file (Input Mode)

Here is a simple example of the VBA FileAttr function. This below example checks the Input mode file. And returns output as 1.

'Check the mode of file (Input Mode)
Sub VBA_FileAttr_Function_Ex1()

    'Variable declaration
    Dim sPath As String
    Dim iFileNum As Integer
    Dim iOutput As Integer
    
    iFileNum = 1
    sPath = "C:\VBAF1\VBA Functions\VBA Text Functions\VBA Functionsa.xlsm"
    
    'Open the file
    Open sPath For Input As iFileNum
    iOutput = FileAttr(iFileNum, 1)
    Close iFileNum
    
    MsgBox "Input  Mode of file : " & iOutput, vbInformation, "VBA FileAttr Function"
    
End Sub

Output: Here is the screen shot of the first example output.
VBA FileAttr Function

Example 2: Check the mode of file (Output Mode)

Here is a simple example of the VBA FileAttr function. This below example checks the Output mode file. And returns output as 2.

'Check the mode of file (Output Mode)
Sub VBA_FileAttr_Function_Ex2()

    'Variable declaration
    Dim sPath As String
    Dim iFileNum As Integer
    Dim iOutput As Integer
    
    iFileNum = 1
    sPath = "C:\Someswari\VBAF1\VBA Functions\VBA Text Functions\VBA Functionsa.xlsm"
    
    'Open the file
    Open sPath For Output As iFileNum
    iOutput = FileAttr(iFileNum, 1)
    Close iFileNum
    
    MsgBox "Output Mode of file : " & iOutput, vbInformation, "VBA FileAttr Function"
    
End Sub

Output: Here is the screen shot of the second example output.
VBA FileAttr Function

Example 3: Check the mode of file (Random Mode)

Here is a simple example of the VBA FileAttr function. This below example checks the Random mode file. And returns output as 4.

'Check the mode of file (Random Mode)
Sub VBA_FileAttr_Function_Ex3()

    'Variable declaration
    Dim sPath As String
    Dim iFileNum As Integer
    Dim iOutput As Integer
    
    iFileNum = 1
    sPath = "C:\Someswari\VBAF1\VBA Functions\VBA Text Functions\VBA Functionsa.xlsm"
    
    'Open the file
    Open sPath For Random As iFileNum
    iOutput = FileAttr(iFileNum, 1)
    Close iFileNum
    
    MsgBox "Random Mode of file : " & iOutput, vbInformation, "VBA FileAttr Function"
    
End Sub

Output: Here is the screen shot of the third example output.
VBA FileAttr Function

Example 4: Check the mode of file (Append Mode)

Here is a simple example of the VBA FileAttr function. This below example checks the Append mode file. And returns output as 8.

'Check the mode of file (Append Mode)
Sub VBA_FileAttr_Function_Ex4()

    'Variable declaration
    Dim sPath As String
    Dim iFileNum As Integer
    Dim iOutput As Integer
    
    iFileNum = 1
    sPath = "C:\Someswari\VBAF1\VBA Functions\VBA Text Functions\VBA Functionsa.xlsm"
    
    'Open the file
    Open sPath For Append As iFileNum
    iOutput = FileAttr(iFileNum, 1)
    Close iFileNum
    
    MsgBox "Append Mode of file : " & iOutput, vbInformation, "VBA FileAttr Function"
    
End Sub

Output: Here is the screen shot of the fourth example output.
VBA FileAttr Function

Example 5: Check the mode of file (Binary Mode)

Here is a simple example of the VBA FileAttr function. This below example checks the Binary mode file. And returns output as 32.

'Check the mode of file (Binary Mode)
Sub VBA_FileAttr_Function_Ex5()

    'Variable declaration
    Dim sPath As String
    Dim iFileNum As Integer
    Dim iOutput As Integer
    
    iFileNum = 1
    sPath = "C:\Someswari\VBAF1\VBA Functions\VBA Text Functions\VBA Functionsa.xlsm"
    
    'Open the file
    Open sPath For Binary As iFileNum
    iOutput = FileAttr(iFileNum, 1)
    Close iFileNum
    
    MsgBox "Binary Mode of file : " & iOutput, vbInformation, "VBA FileAttr Function"
    
End Sub

Output: Here is the screen shot of the fifth example output.
VBA FileAttr Function

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

Leave a Reply