VBA Name Function

VBA Name Function

Overview of VBA Name Function:


VBA Name function is categorized as File and Direcetory function. This built-in VBA RmDir 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 use Macro Codes

Here are the step by step instructions to use the Name Function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.

VBA File & Directory Functions List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *