Tuesday, June 4, 2019

Rename excel sheet using VBScript


VBSCript code to rename excel sheet;Rename Excel Sheet VBScript;Excel sheet rename using VBScript;Excel sheet rename programmatically VBScript




How to rename Excel worksheet from VBScript


1) The following code renames an excel worksheet in VBScript.

To rename an excel sheet we need to open the excel file, rename the sheets, save and close it. Now we can start coding to open and rename the first excel sheet from "Sheet1" to "NewName".


1. Create a file ExcelRename.vbs and paste the below VBScript  Code

':: How to rename Excel worksheet from VBScript
'----------------------------------------------

'create the excel object
	Set objExcel = CreateObject("Excel.Application") 

'view the excel program and file, set to false to hide the whole process
	objExcel.Visible = True

'turn off screen alerts
        objExcel.DisplayAlerts = False

'open an excel file (make sure to change the location) .xls for 2003 or earlier
	Set objWorkbook = objExcel.Workbooks.Open("C:\Viji\Test.xlsx")

'Use objWorkbook.Worksheets.count to get the count of sheets
'Get the first sheet
        Set objWorksheet = objWorkbook.Worksheets(1) 
'Rename the sheet
       objWorksheet.Name = "NewName"

'save the existing excel file. use SaveAs to save it as something else
	objWorkbook.Save

'close the workbook
	objWorkbook.Close 

'exit the excel program
	objExcel.Quit

'release objects
	Set objExcel = Nothing
	Set objWorkbook = Nothing

2. Double click ExcelRename.vbs to execute. The sheet has been renamed from "Sheet1" to "NewName"


After rename



It's so simple !!!! Happy Coding.

6 comments:

  1. very nice and provide me informative content thanks for sharing for more information about Rename Sheet in Excel

    ReplyDelete
  2. Very good piece my friend, I like it's complete even with open and close instance.

    ReplyDelete
  3. Thank you so much! I came to your script since I needed an specific information on how to rename sheets using VBS! thank you for the complete content

    ReplyDelete
  4. This blog is really great. The information here will surely be of some help to me. Thanks!. names ideas

    ReplyDelete
  5. I got too much interesting stuff on your blog. I guess I am not the only one having all the enjoyment here! Keep up the good work. tech company names

    ReplyDelete