VBA Name Function

VBA Name Function

VBA Name function is categorized as File and Directory function. This built-in VBA Name function moves the specified file to the new directory or folder in Excel VBA. This function works like rename of an existing file, folder, or directory. If specified file is not available, it returns an error. If we try to rename already opened file, throws an error.

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

Syntax of VBA Name Function

The syntax of the Name Function in VBA is

Name OldPath As NewPath

The RmDir Function doesn’t return any value. It renames an existing folder or file.

Parameters or Arguments:

The Name function/statement has two arguments in Excel VBA.
where
OldPath: It is a mandatory string parameter. The OldPath argument represents the folder or file path to rename.
NewPath: It is a mandatory string parameter. The NewPath argument represents the folder or file path.

Where we can apply or use VBA Name Function?

We can use this Name 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: Move a File

Here is a simple example of the VBA Name function. This below example moves a file from source location to destination location. Once moved file to destination, file is not available at source location.

'Move a File
Sub VBA_Name_Function_Ex1()

    'Variable declaration
    Dim sFileName1 As String, sFileName2 As String
    
    sFileName1 = "C:\Test\Temp.xlsm"
    sFileName2 = "C:\Test1\Temp.xlsm"
    
    Name sFileName1 As sFileName2
        
    MsgBox "File moved to : " & vbCrLf & sFileName2, vbInformation, "VBA Name Function"
    
End Sub

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

Example 2: Move a File(Returns an Error)

Here is a simple example of the VBA Name function. This below example returns an error. Because specified file is not available at source location.

'Move a File(Returns an Error)
Sub VBA_Name_Function_Ex2()

    'Variable declaration
    Dim sFileName1 As String, sFileName2 As String
    
    'Note: lets assume specified file 'sFileName1' is not avaialble
    sFileName1 = "C:\Test\Temp.xlsm"
    sFileName2 = "C:\Test1\Temp.xlsm"
    
    Name sFileName1 As sFileName2
        
    MsgBox "File moved to : " & vbCrLf & sFileName2, vbInformation, "VBA Name Function"
    
End Sub

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

Example 3: Move and ReName a File

Here is a simple example of the VBA Name function. This below example moves and renames a file from source location to destination location. Once moved and renamed file at destination, file is not available at source location.

'Move and ReName a File
Sub VBA_Name_Function_Ex3()

    'Variable declaration
    Dim sFileName1 As String, sFileName2 As String
    
    sFileName1 = "C:\Test\OldName.xlsm"
    sFileName2 = "C:\Test1\NewName.xlsm"
    
    Name sFileName1 As sFileName2
        
    MsgBox "File moved and Renamed to : " & vbCrLf & sFileName2, vbInformation, "VBA Name Function"
    
End Sub

Output: Here is the screen shot of the third example output.
VBA Name 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