Worksheet_Change

truerip

New Member
Joined
Feb 26, 2015
Messages
19
I know that I'm missing something simple and am hoping that someone can point out what I'm missing :smile:

Here goes:
I have an 8 tab spreadsheet (Excel 2013)
Formulas in Sheet1 151:A157 update with DAY()+1 based on the date entered into Sheet1 A1.
So with 9/8/18 entered into A1, A151=9; A152=10; A153=11 and so on.
I want my code to rename Sheet2 thru Sheet8 based on the contents of A151:A157 when I change the date in A1.
Example Sheet1 A1 = 9/8/18
Sheet1 A151 = 9 then rename Sheet2 to 9
Sheet1 A152 = 10 then rename Sheet3 to 10
Sheet1 A153 = 11 then rename Sheet4 to 11
and so on.

The code below works if I enter a value into A151:A157 directly, but not automatically from formulas when they change based off of new date entered into A1.

Any help would be greatly appreciated. Thanks!


Code:
Private Sub Worksheet_Change(ByVal Target As Range)


Dim Cells As Range


On Error Resume Next



For Each cell In Target
    If cell.Address = "$A$151" Then
        Sheets(2).Name = cell.Text
    ElseIf cell.Address = "$A$152" Then
        Sheets(3).Name = cell.Text
    ElseIf cell.Address = "$A$153" Then
        Sheets(4).Name = cell.Text
    ElseIf cell.Address = "$A$154" Then
        Sheets(5).Name = cell.Text
    ElseIf cell.Address = "$A$155" Then
        Sheets(6).Name = cell.Text
    ElseIf cell.Address = "$A$156" Then
        Sheets(7).Name = cell.Text
    ElseIf cell.Address = "$A$157" Then
        Sheets(8).Name = cell.Text
       
        
    End If
  
     
Next cell

If Err > 0 Then MsgBox "Sheet named renamed due to name conflict. Please try again."
End Sub
 
Last edited by a moderator:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Formulas don’t trigger a Change event. You should monitor the input cell A1 and then just loop through your range.
 
Upvote 0
Something like this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


Dim n as long
On Error Resume Next


If not intersect(Target, Range("A1")) Is Nothing then
For n = 1 to 7
    Sheets(n + 1).Name = Range("A150").Offset(n).Text
next n
end if
If Err > 0 Then MsgBox "Sheet named renamed due to name conflict. Please try again."
End Sub
 
Upvote 0
I was working on this:
Script runs when A1 Changes
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  9/25/2018  4:25:17 PM  EDT
If Not Intersect(Target, Range("A1")) Is Nothing Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
On Error GoTo M
Dim i As Long
Dim x As Long
Dim ans As Long
    x = 151
ans = Month(Target.Value)
For i = 2 To 8
Sheets(i).Name = Cells(x, 1).Value: x = x + 1
Next
End If
Exit Sub
M:
MsgBox "That sheet name may already exist or  is a improper name"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,641
Messages
6,173,506
Members
452,518
Latest member
SoerenB

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