Macro to set tab name equal to cell value

ctish

New Member
Joined
Jan 28, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
I am looking to be able to run a macro that will allow me to choose a folder with all of my workbooks and set the first tab name to equal the value of cell B7. All the workbooks are the same. Ideally it would open a file, make the tab name equal to the text in B7, save-as, close, open the next file and so on until the files have all been run through inside the folder. I am pretty new to the macro world so bear with me. Thanks in advance!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Perhaps something like this which assumes it's B7 on the first sheet that you want to use for the tab name.
VBA Code:
Public Function GetFolder(Optional OpenAt As String, Optional strTitle = "Please select folder") As String

    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .InitialFileName = OpenAt
        .Title = strTitle
        .Show
        If .SelectedItems.Count <> 0 Then
            GetFolder = .SelectedItems(1)
        End If
    End With
    
End Function

Sub ChangeTabName()
Dim wb As Workbook
Dim objFSO As Object
Dim objFile As Object
Dim objFolder As Object
Dim strFolder As String

    strFolder = GetFolder
    
    If strFolder = "" Then
        MsgBox "No folder selected!", vbInformation, "No folder selected"
    End If
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Set objFolder = objFSO.GetFolder(strFolder)
    
    For Each objFile In objFolder.Files
        If objFile.Type Like "*Excel*" Then
            Set wb = Workbooks.Open(objFile.Path)
            wb.Sheets(1).Name = wb.Sheets(1).Range("B7").Value
        End If
    Next objFile
    
End Sub
 
Upvote 0
Looks like it gets hung up on the last line before "End If". The line highlighted is wb.Sheets(1).Name = wb.Sheets(1).Range("B7").Value

The error code I get is Run-time error '13': Type mismatch.
 
Upvote 0
Perhaps something like this which assumes it's B7 on the first sheet that you want to use for the tab name.
VBA Code:
Public Function GetFolder(Optional OpenAt As String, Optional strTitle = "Please select folder") As String

    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .InitialFileName = OpenAt
        .Title = strTitle
        .Show
        If .SelectedItems.Count <> 0 Then
            GetFolder = .SelectedItems(1)
        End If
    End With
  
End Function

Sub ChangeTabName()
Dim wb As Workbook
Dim objFSO As Object
Dim objFile As Object
Dim objFolder As Object
Dim strFolder As String

    strFolder = GetFolder
  
    If strFolder = "" Then
        MsgBox "No folder selected!", vbInformation, "No folder selected"
    End If
  
    Set objFSO = CreateObject("Scripting.FileSystemObject")
  
    Set objFolder = objFSO.GetFolder(strFolder)
  
    For Each objFile In objFolder.Files
        If objFile.Type Like "*Excel*" Then
            Set wb = Workbooks.Open(objFile.Path)
            wb.Sheets(1).Name = wb.Sheets(1).Range("B7").Value
        End If
    Next objFile
  
End Sub
Disregard my previous reply. The macro works to place the value of B7 once I eliminated an unneeded extra sheet in each workbook. The only thing now is I need it to run so that once the function of placing the value of B7 in the tab name, I need it to save the file and close it. As is, it opens the file, changes the tab name, then leaves it unsaved and open. Thanks for the quick response!
 
Upvote 0
Oops, sorry about that - forgot to add the, quite important, part to close the workbooks.:eek:

I've added it in the code below.
VBA Code:
Option Explicit

Public Function GetFolder(Optional OpenAt As String, Optional strTitle = "Please select folder") As String

    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .InitialFileName = OpenAt
        .Title = strTitle
        .Show
        If .SelectedItems.Count <> 0 Then
            GetFolder = .SelectedItems(1)
        End If
    End With
   
End Function

Sub ChangeTabName()
Dim wb As Workbook
Dim objFSO As Object
Dim objFile As Object
Dim objFolder As Object
Dim strFolder As String

    strFolder = GetFolder
   
    If strFolder = "" Then
        MsgBox "No folder selected!", vbInformation, "No folder selected"
    End If
   
    Set objFSO = CreateObject("Scripting.FileSystemObject")
   
    Set objFolder = objFSO.GetFolder(strFolder)
   
    For Each objFile In objFolder.Files
        If objFile.Type Like "*Excel*" Then
            Set wb = Workbooks.Open(objFile.Path)
            wb.Sheets(1).Name = wb.Sheets(1).Range("B7").Value
            wb.Close SaveChanges:=True
        End If
    Next objFile
   
End Sub
 
Upvote 0
Solution
Oops, sorry about that - forgot to add the, quite important, part to close the workbooks.:eek:

I've added it in the code below.
VBA Code:
Option Explicit

Public Function GetFolder(Optional OpenAt As String, Optional strTitle = "Please select folder") As String

    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .InitialFileName = OpenAt
        .Title = strTitle
        .Show
        If .SelectedItems.Count <> 0 Then
            GetFolder = .SelectedItems(1)
        End If
    End With
  
End Function

Sub ChangeTabName()
Dim wb As Workbook
Dim objFSO As Object
Dim objFile As Object
Dim objFolder As Object
Dim strFolder As String

    strFolder = GetFolder
  
    If strFolder = "" Then
        MsgBox "No folder selected!", vbInformation, "No folder selected"
    End If
  
    Set objFSO = CreateObject("Scripting.FileSystemObject")
  
    Set objFolder = objFSO.GetFolder(strFolder)
  
    For Each objFile In objFolder.Files
        If objFile.Type Like "*Excel*" Then
            Set wb = Workbooks.Open(objFile.Path)
            wb.Sheets(1).Name = wb.Sheets(1).Range("B7").Value
            wb.Close SaveChanges:=True
        End If
    Next objFile
  
End Sub
That's amazing! Works like a charm! This saves me hours of time. Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,443
Members
448,898
Latest member
drewmorgan128

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