Forum Discussion
Rename Cell to Various Different Sheet Titles
Hi,
I am trying to create a formula that allows me to have cells equal to the names of different sheets. I have read several interesting posts that created solutions for having a cell automatically change to the name of whatever its sheet is, however, I am trying to create a table that automatically fills out the rows as I add new tabs.
For example: I add a new sheet and name it "March", the next column title in my table will be "March".
Having to do it manually at the scale required would be quite difficult. I believe this may require the use of VBA, and my attempts to teach myself were not very fruit-full.
Is this possible?
Thanks in advance.
You can achieve it without VBA coding. Try the following in name manager and call that name to any cell. See the attached file.
Important Note: You must enable Excel 4.0 Macros from macro settings.
=TEXTAFTER(GET.WORKBOOK(1)&T(NOW()),"]")
7 Replies
- JKPieterseSilver Contributor
If you are on M365, you can use this lambda formula, which does not require saving the file as macro-enabled file: https://jkp-ads.com/articles/excel-lambda-function-examples.aspx#name_of_worksheet_in_cell
- mathetesSilver Contributor
You've gotten a number of proposals for how to achieve what you asked, and if one (or more) of those satisfy you fully, fine.
When I read requests like this--where it's obvious (or seems to be so; forgive me if I'm wrong) that you are creating a sheet for each month of the year--I will often first turn the question back and ask "Why?" Why, specifically, are you designing this workbook with a separate sheet for each month?
I've seen many a time when that approach is undertaken because that's how we did it on paper ledger sheets, so it just makes sense to automate it with the same structure, assuming Excel can make (as it can) the links to summarize from monthly into quarterly or annual reports. HOWEVER, Excel has marvelous capabilities to summarize data on a monthly, quarterly, or annual basis, working from a single database of transactions or activities. And it's generally more straightforward if you do things that way.
Think of it as letting Excel do the heavy lifting. If you've continued the task of "manually" storing data on a month-by-month basis, you're doing a fair amount of work unnecessarily.
So my next question: is it possible that you could benefit from re-thinking the basic design of your workbook. (Just a suggestion. Feel free to totally ignore.)
- Harun24HRBronze Contributor
mathetes Sir, nice comment. It is strongly recommended to store data in a single sheet in logical manner and prepare report on the run time as need. All database works on same concept, even file level database like Excel also is suitable to work on same concept. I always suggest my users to thick simply to store data in one place and make report using formulas, filtering's, pivot tables etc.
- Harun24HRBronze Contributor
You can achieve it without VBA coding. Try the following in name manager and call that name to any cell. See the attached file.
Important Note: You must enable Excel 4.0 Macros from macro settings.
=TEXTAFTER(GET.WORKBOOK(1)&T(NOW()),"]")
- danny1968Copper Contributor
Very simple and elegant solution, thank you.
- SnowMan55Bronze Contributor
"Having to do it manually at the scale required would be quite difficult."
That seems unlikely. The "scale" is one new worksheet at a time.But perhaps you meant that manually creating the initial list of existing worksheets would be tedious. Yes, it could be (for example, if you inherited a workbook with hundreds of worksheets). This post suggests how that could be done readily.
A better-behaved macro would not modify an entire column on the active worksheet (however important that may be!) without user confirmation, leaving no way to undo the data change. So consider this code instead:
Sub LoadListOfWorksheets() ' This macro creates a list of the worksheets in the active workbook ' into column A (see the constant below) of the active worksheet, ' subject to confirmation by the user. Const strCOLUMN_ID = "A" 'for output Const in4FIRST_ROW_TO_USE As Long = 2 'for output Dim objWkbook As Workbook Dim objListWksht As Worksheet Dim strFullColumn As String 'identifies the range Dim in4CellsWithContent As Long '…within the range ' -- Related to the user confirmation: Dim dstrMessage As String Dim in4Icon As Long Dim in4UserResponse As VbMsgBoxResult ' -- Indexes, etc.: Dim in4Wksht As Long '…within the Sheets collection Dim in4OutputRow As Long '---- Capture information and do preparations. Set objWkbook = ActiveWorkbook Set objListWksht = ActiveSheet ' strFullColumn = strCOLUMN_ID & ":" & strCOLUMN_ID '---- Get user confirmation. in4CellsWithContent = WorksheetFunction.CountA( _ objListWksht.Range(strFullColumn)) dstrMessage = "Is it OK to put the worksheet names into column " _ & strCOLUMN_ID _ & vbCrLf & " of worksheet " & objListWksht.Name _ & vbCrLf & " in workbook " & objWkbook.Name & "?" If in4CellsWithContent > 0 Then dstrMessage = dstrMessage & vbCrLf & vbCrLf _ & "WARNING: Column " & strCOLUMN_ID & " has " _ & Format$(in4CellsWithContent, "#,###,###,##0") _ & " cell" & IIf(in4CellsWithContent = 1, "", "s") _ & " that contain" & IIf(in4CellsWithContent = 1, "s", "") _ & " some content. That content will be erased !!" in4Icon = vbExclamation Else in4Icon = vbQuestion End If in4UserResponse = MsgBox(dstrMessage, in4Icon Or vbYesNo _ Or vbDefaultButton2, "Load List of Worksheets") If in4UserResponse = vbNo Then Exit Sub '---- To improve performance of the remaining code, turn off ' screen updating. Application.ScreenUpdating = False '---- Clear any existing content. With objListWksht.Range(strFullColumn) .ClearContents .NumberFormat = "@" 'to prevent Excel from converting certain _ worksheet names to a date or number or Boolean End With '---- Create an unsorted list of all worksheet names. in4OutputRow = in4FIRST_ROW_TO_USE For in4Wksht = 1 To objWkbook.Sheets.Count objListWksht.Range(strCOLUMN_ID & in4OutputRow).Value = _ objWkbook.Sheets(in4Wksht).Name ' in4OutputRow = in4OutputRow + 1 Next in4Wksht '---- Restore setting(s). Application.ScreenUpdating = True End Sub
I will leave it as an exercise for you to write worksheet names into one row, rather than into one column.
Note that this macro would not even have to be stored in the relevant workbook. You could place it into your Personal.xlsb workbook.
As for automatically updating a list of workbooks (whether in a range of cells or an Excel table), that's complicated. See the attached workbook for more information and example VBA code.
Edit: fixing another example of the forum dropping the file that I attached
Edit: trying a third time
Edit: and a fourth time
- m_tarlerBronze Contributor
So my approach would be to reserve a cell on each sheet for the sheet name. For example cell A1 could be:
=LET(path,CELL("filename",A1),RIGHT(path,LEN(path)-SEARCH("]",path)))
Then make sure you have all the sheets 'bound' by a known sheet name. For example lets say you always have JAN first and the last sheet is SUMMARY and you insert months inbetween as the year goes by then:
=DROP(HSTACK('JAN:SUMMARY'$A$1),,-1)
where that will 'stack' all the sheet names (each found in cell A1 of each sheet) and then drop that last one from this SUMMARY sheet.