Automatically rename new Sheet

happydonut

Board Regular
Joined
Nov 28, 2019
Messages
57
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a request and was hoping someone could help.

I have a Macro that turns data in Excel to a Pivot table.

When that is done, I can double click on a value in the Pivot Table, and a more detailed view shows. At the same time, a new Sheet gets created and gets opened. Is it possible to rename this new Sheet automatically? Preferably to the value on the same row, see example:

Column AColumn B
Row 1Salary100 000

(When double clicking the amount, it will open a new Sheet, which should named Salary).
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
So the code below is pretty basic, but what it does is create a new sheet in the relevant workbook when you double click in a cell. The sheet name will be whatever is the contents of the cell you double-clicked. It needs to be added to the ThisWorkbook module (link).

VBA Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim NewName As String, NewWS As Worksheet
NewName = Target.Value
Set NewWS = ThisWorkbook.Sheets.Add
NewWS.Name = NewName
End Sub

I should stress that there are a number of problems with using this code as-is. There should be some form of error-handling to deal with a variety of foreseeable problems - what if you happen to double-click on an empty cell? what if there already exists a sheet with the same name? etc. I would add that this double-click function should be limited to only a certain part of a certain sheet (i.e., the range of the pivot table).

Anyway, this at least demonstrates that it's doable.
 
Upvote 0
Thanks for reply.

Well, I want the sheet name to be named after a certain cell on the same row as I double click.
As my example above, if I double click on the cell with the value 100 000, I want the Sheet name that has been created to automatically named Salary (e.g. the cell left to the one I doubleclick)
 
Upvote 0
Ok. Try this:

VBA Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim NewName As String, NewWS As Worksheet
NewName = Target.Offset(0,-1).Value
Set NewWS = ThisWorkbook.Sheets.Add
NewWS.Name = NewName
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,506
Messages
6,125,189
Members
449,213
Latest member
Kirbito

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