Rename Sheet Name based on Workbook name

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
201
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
just a quick question, is there a way to rename sheet name if workbook contains certain string?
i.e. MainOfficeReport.xls
if it contains the word "office" in the workbook name then change the sheet name to Office
BTW, default sheetname for all report have the same name thus the need to change it when opening and merging
I hope we have one.... :)
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Assuming that you are on the sheet whose name you want to change when you run the code, try this:
VBA Code:
Sub RenameSheet()
    If InStr(1, LCase(ActiveWorkbook.Name), "office") > 0 Then
        ActiveSheet.Name = "Office"
    End If
End Sub
 
Upvote 0
Assuming that you are on the sheet whose name you want to change when you run the code, try this:
VBA Code:
Sub RenameSheet()
    If InStr(1, LCase(ActiveWorkbook.Name), "office") > 0 Then
        ActiveSheet.Name = "Office"
    End If
End Sub
thanks mate, that will do when the workbook is currently open. but what I would like to do is select multiple files on a directory then test/match each workbook
if one workbook contains a certain word, it will rename the workbook to said keyword
lets say we have 2 workbook on a folder and the keyword is "main" if workbook inside folder named is as follows "20230511maindata.xlsx" and 20230510extendeddata.xlsx" then workbook "20230511maindata.xlsx" will become "main.xlsx"
does not matter if its open or not as long as we can change the workbook name
 
Upvote 0
Did some searching and tweaking and finally got what I wanted!
if anyone with the same problem the following code works with "what" I needed....

VBA Code:
Sub RenameMyFile()
'   AirForceOne :)
'   Microsoft Scripting Runtime is REQUIRED!

    Dim MyFSO As Scripting.FileSystemObject
    Dim objDir As Folder
    Dim FilePath As String
    Dim objFile As File
    
    FilePath = "C:\ReportGeneratorV.3\SOURCE\"    
    Set MyFSO = CreateObject("Scripting.FileSystemObject")
    
    If MyFSO.FolderExists(FilePath) Then    
        Set objDir = MyFSO.GetFolder(FilePath)
        
        For Each objFile In MyFSO.GetFolder(FilePath).Files        
            If UCase(objFile.Name) Like "*MAIN*" Then
                Debug.Print objFile.Name
                MyFSO.CopyFile objDir.PatH & "\" & objFile.Name, objDir.PatH & "\Exported.xlsx", True
                MyFSO.DeleteFile (objFile.PatH)
            End If            
        Next objFile        
    End If
    
End Sub
 
Upvote 0
Solution
Glad you have it sorted and thanks for letting us know. However, it seems that the requirement has changed considerably or else the original request was very much mis-stated as there is a significant difference between a worksheet and a workbook. ;)
oops sorry about that mate
in the heat of the moment I totally forgot it! anyway as I was replying to @Joe4 as I was simultaneously cleaning my working code both for renaming sheet based on workbook value, I was actually working on the module of renaming my workbook inside a folder thus the confusion! the code on post # 4 was the result of a working code after renaming sheet based on workbook value
Again apologies on my blunder...
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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