Tuesday, January 22, 2019

How to Rename Excel Work Sheet using VBA Code | How to Rename Excel Work Sheet using Macro

How to Rename Excel Work Sheet using VBA Code | How to Rename Excel Work Sheet using Macro


1. Open active excel sheet 

2. Press Alt + F11 and Visual Basic Application window will open.

3. Go to Insert Menu and select new Module.

4. In new Module insert the below VBA Code and save it.

5. Before do the above process, excel sheet must save as Macro-Enabled.

6. To do Macro Enabled, open Excel Sheet and Click on the Office Button which is at left top corner on excel sheet.

7.  Select Save as and again select Macro Enabled Worksheet and then save it.

How to Rename Excel Work Sheet using VBA Code | How to Rename Excel Work Sheet using Macro


Sub renameWorksheet()
    Dim wb As Workbook
    Dim sh As Worksheet

    Dim newSheetName As String
    newSheetName = "March"

    Dim fPath As String

    fPath = "C:\Users\vmishra\Desktop\myfile.xlsx"
    Set wb = workbooks.Open(Filename:=fPath)
    ' Rename the sheet name of the 1st sheet
    Set sh = wb.Worksheets(1)
    sh.Name = newSheetName

End Sub

No comments:

Post a Comment