VBA not working with a formula

amberdstjohn

New Member
Joined
May 6, 2024
Messages
2
Office Version
  1. 2011
Platform
  1. Windows
Hi,

I have a VBA code that I am using that changes the tab name when a cell is updated. This works great when you type data into the cell, but I am mainly needing it to work with a formula. The formula is just a vlookup, and I tried using =(CELL("contents",$O$3), but that doesn't work either. Any tips on how to make the code work with a formula?

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  If Target.Address(0, 0) = "A1" Then
    Sh.Name = Sh.Range("A1").Value
  End If
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try using the Workbook_SheetCalculate event handler instead...


However, you should probably check whether a sheet with the same name already exists before changing the name of the sheet tab. Otherwise you'll get an error if one already exists.

Hope this helps!
 
Upvote 0
Try using the Workbook_SheetCalculate event handler instead...


However, you should probably check whether a sheet with the same name already exists before changing the name of the sheet tab. Otherwise you'll get an error if one already exists.

Hope this helps!
Thanks for this, but it still isn't updating when the formula updates. I am new to using VBA, so it could be something I am doing wrong.
 
Upvote 0
The following code must be placed in the code module called ThisWorkbook. This code will be executed whenever any worksheet is recalculated. First it checks to make that cell A1 does not contain an error. Then it makes sure that it's not blank. And then it makes sure that a sheet with the same name does not already exist. If all true, it renames the worksheet. Otherwise it's ignored. Does this help?

VBA Code:
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)

    Dim targetCell As Range
    Set targetCell = Sh.Range("A1")
    
    If Not IsError(targetCell) Then
        If Len(targetCell) > 0 Then
            If Not SheetExists(targetCell.Value, ThisWorkbook) Then
                Sh.Name = targetCell.Value
            End If
        End If
    End If
    
End Sub

Private Function SheetExists(ByVal sSheetName As String, Optional ByVal wb As Workbook = Nothing) As Boolean

    Dim x As Object
    
    If wb Is Nothing Then
        Set wb = ActiveWorkbook
    End If
    
    On Error Resume Next
    Set x = wb.Sheets(sSheetName)
    On Error GoTo 0
    
    SheetExists = Not x Is Nothing
    
End Function
 
Upvote 0

Forum statistics

Threads
1,216,105
Messages
6,128,859
Members
449,472
Latest member
ebc9

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