VBA Macro to Rename a Worksheet in all Excel Files in a Folder

nelle06

New Member
Joined
Jul 2, 2013
Messages
11
I am really new to VBA. I am trying to use the code below to rename one worksheet in multiple files within one folder, but I keep getting a Subscript Out of Range error at the underlined line. The names are accurate, so that isn't the issue. Can anyone tell me what else could be wrong?

Sub RenameTab_Revised()

strPath = "C:\Documents and Settings\Desktop\Testing VB Code\"

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(strPath)

For Each objFile In objFolder.Files

If objFso.GetExtensionName(objFile.Path) = "xls" Then
Set objWorkbook = objExcel.Workbooks.Open(objFile.Path)
Sheets("Local EMC Support").Select
Sheets("Local EMC Support").Name = "TIF Debt"
objWorkbook.Close True
End If

Next
End Sub

Thanks!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
That says to me that the sheet does not exist.

Run this and see what happens.

Code:
Sub RenameTab_Revised()
Dim ws As Worksheet


strPath = "C:\Documents and Settings\Desktop\Testing VB Code\"


Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False


Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(strPath)


For Each objFile In objFolder.Files


If objFso.GetExtensionName(objFile.Path) = "xls" Then
Set objWorkbook = objExcel.Workbooks.Open(objFile.Path)


On Error Resume Next
Set ws = Sheets("Local EMC Support")
On Error GoTo 0


    If ws Is Nothing Then
        MsgBox "Sheet" & ws.Name & " does not exist in " & objWorkbook.Name
        objWorkbook.Close True
    Else
    Sheets("Local EMC Support").Select
    Sheets("Local EMC Support").Name = "TIF Debt"
    objWorkbook.Close True
    End If
End If


Next
End Sub
 
Upvote 0
When I get to the line - MsgBox "Sheet" & ws.Name & " does not exist in " & objWorkbook.Name - I get a runtime error '91': Object variable or With block variable not set.
 
Upvote 0
Forgive me, a moment of stupidity:

Code:
Sub RenameTab_Revised()
Dim ws As Worksheet


strPath = "C:\Documents and Settings\Desktop\Testing VB Code\"


Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True


objExcel.DisplayAlerts = False


Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(strPath)


For Each objFile In objFolder.Files


If objFso.GetExtensionName(objFile.Path) = "xls" Then
Set objWorkbook = objExcel.Workbooks.Open(objFile.Path)


On Error Resume Next
Set ws = Sheets("Local EMC Support")
On Error GoTo 0


    If ws Is Nothing Then
        MsgBox "The Worksheet does not exist in " & objWorkbook.Name
        objWorkbook.Close True
    Else
    Sheets("Local EMC Support").Select
    Sheets("Local EMC Support").Name = "TIF Debt"
    objWorkbook.Close True
    End If
End If


Next
End Sub
 
Upvote 0
That code produced the message that the sheet does not exist in the file, but the sheet does exist. Is there anything that might cause this to happen?
 
Upvote 0
Retype the sheet name and then run again. I wonder if there are some hidden characters there somewhere.
 
Upvote 0
I have tried re-typing and copying and pasting the worksheet name but the same error comes up. Does it matter if the sheet is protected?
 
Upvote 0
I have tried re-typing and copying and pasting the worksheet name but the same error comes up. Does it matter if the sheet is protected?

Sheet protection would not cause this error.

Could you save another copy of the workbook with all the sensitive data removed and upload or email a copy to me?
 
Upvote 0
There is too much sensitive information to do that. This gets me close enough. Thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,215,408
Messages
6,124,727
Members
449,185
Latest member
ekrause77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top