VBA to rename sheets based on cell value (with duplicates)

Excel_Blonde

New Member
Joined
Aug 8, 2018
Messages
44
Hi All,

I've seen many posts with questions similar to my own but cant find one to solve my problem.

In fact I have a few issues which I will detail here, any solutions to any of them will be extremely helpful.

I have a report I export into excel, the report is multiple sheets each sheet being an individual record. I want to carry out analysis on this but the way the information is displayed isn't great. I have tried many ways of addressing this but having no luck...

1. So the first issue is I need to rename each sheet with a given cell value...typically (I4), there will be duplicates so need to add incremented suffixes. I say typically because it isn't always I4, so that opens up the next problem to be resolved. I would like to use a macro to look for a given term e.g Name and loop through all columns (Within that row) until a value is found and then move that value to I4 (which I guess needs to happen before the rename sequence).
2. Then I would like to create a new sheet at the beginning of the workbook named 'list' listing all worksheet names.
3. I then within each sheet need to look for the first instance of 'total' and loop through all columns (within that row) returning the first 4 values found within the new list sheet.

e.g
List sheet
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sheet Name[/TD]
[TD]Planned S[/TD]
[TD]Planned R[/TD]
[TD]Actual S[/TD]
[TD]Actual R[/TD]
[/TR]
[TR]
[TD]Fred[/TD]
[TD]2[/TD]
[TD]5[/TD]
[TD]8[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]Tom[/TD]
[TD]12[/TD]
[TD]5[/TD]
[TD]5[/TD]
[TD]8[/TD]
[/TR]
[TR]
[TD]Geoff[/TD]
[TD]5[/TD]
[TD]8[/TD]
[TD]9[/TD]
[TD]12[/TD]
[/TR]
</tbody>[/TABLE]



I hope you are still with me, I am relatively new to VBA but have been able to create several useful Macro's, I eventually figure things out with help of forums like this so any help would be appreciated and I'm sure I can fill in the blanks.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Update:

I have managed to generate the code to add the new sheet named 'list' with a list of all other sheets, I've also managed to rename the sheets where the value is in I4 including duplicates. The main issue now is where the data isn't actually in I4. Is there any way of looking at a range rather than I4? eg G4:K4?

I'm currently using the following:

Sub RenameSheets1()


Dim ws As Worksheet, wscount As Integer, wsname As String, dict
Set dict = CreateObject("scripting.Dictionary")


For Each ws In Sheets
If ws.Name <> "List" Then
If ws.Range("I4") <> "" Then
wsname = Replace(ws.Range("I4"), "/", "")
wscount = IIf(dict.Exists(wsname), dict(wsname) + 1, 1)
dict(wsname) = wscount
If wscount = 1 Then
ws.Name = wsname
Else
If wscount = 2 Then Sheets(wsname).Name = wsname & -1
End If
End If
End If
Next ws
End Sub


Please feel free to advise how I can tidy this up as I've just pieced codes together.
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,875
Members
452,949
Latest member
Dupuhini

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