It is not unusual to keep track of monthly information, of one sort or another, in a workbook. You might be tracking expenses, sales, inventory movements, stock prices, or any of a thousand other things. When you start a new month, you may make a copy of the previous month's workbook and then look for a way to make changes to the month name that appears in various places in the newly created copy.
If the month name you want to change is stored as text within various worksheets, you can use Excel's find and replace feature to make the changes. Just follow these steps:
If these steps do not change a particular month name as it appears in your workbook, it could be because the month name is not actually text, but a date value formatted to show only the month. In that case, you cannot use Find and Replace; instead you must simply change the date value stored in the cell.
If you want a quick way to change the month names in the worksheet tabs, that is a bit trickier. Excel's find and replace feature won't find or replace the text in tab names. Normally they need to be done by hand, but if you have many of them, you may want to create a macro that will do the changing for you. The following macro prompts you for the text you are searching for and the text you want to replace it with. Then, it steps through each worksheet tab and makes the changes for you.
Sub TabReplace() Dim I As Integer, J As Integer Dim sFind As String Dim sReplace As String Dim sTemp As String sFind = InputBox("Text to find?") sReplace = InputBox("Replace it with?") If (sFind & sReplace) = "" Then Exit Sub For I = 1 To Sheets.Count sTemp = Sheets(I).Name J = InStr(sTemp, sFind) While J > 0 sTemp = Left(sTemp, J - 1) & sReplace _ & Mid(sTemp, (J + Len(sFind))) J = InStr(sTemp, sFind) Wend If sTemp <> Sheets(I).Name Then Sheets(I).Name = sTemp End If Next I End Sub
Even though the steps (and macro) presented here can make the job of updating your workbook easier, it may be easier still to simply rethink how you do your workbook. It may be easier to set up a cell to contain the current month's name, and then reference that name in the appropriate cells throughout the workbook. Then, all you need to do is change the month name in a single cell, and it will be changed elsewhere, automatically. In other ExcelTips you even learned how you can dynamically change a tab name based on the contents of a particular cell.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12275) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Changing Months in a Workbook.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Separating text values in one cell into a group of other cells is a common need when dealing with text. Excel provides a ...
Discover MoreDelete a cell or a range of cells, and Excel needs to figure out how to rearrange the void left by the deletion. You can ...
Discover MoreWhen you import information originating in a different program, Excel may not do the best job at figuring out what ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-01-31 17:44:41
Ronmio
If the month is stored as part of a date value, you can still use search and replace. For instance, to change all the Augusts to Septembers, search for "8/" and replace it with "9/". However, if you do a Replace All, it will also replace any DAYS of the month that end in 8. If that's an issue, use Find Next and Replace to just change any "8/" that is actually an August.
2019-11-08 10:44:07
Willy Vanhaelen
@Robert Lohman
You are wrong indeed. Your saying that my macro assigns the same name to all sheets makes no sense because, as you mention, Excel doesn't allow that. So it would not be possible to run the macro because it would chrash at once. On my pc it runs just fine and does a nice job anyway.
You are right to suggest that the name change needs to be inside the loop and that's exactly where it is done of course.
I have the impression you even didn't test the macro, so try it -:).
2019-11-06 13:35:44
Robert Lohman
Sorry Willie. You are usually the correct answer but this time I think you missed the point. Your Macro assigns the same name to all sheets, which you well know can not be done. I think the name change needs to be inside the loop. I could be wrong. Please advise
2016-07-23 11:31:43
Willy Vanhaelen
The macro in is typ is way to complex and can be simplified a lot:
Sub TabReplace()
Dim sht As Object, sFind As String, sReplace As String
sFind = InputBox("Text to find?")
sReplace = InputBox("Replace it with?")
If (sFind & sReplace) = "" Then Exit Sub
For Each sht In Sheets
sht.Name = Replace(sht.Name, sFind, sReplace)
Next sht
End Sub
The one-line Replace function in my version does the job of the whole While ... Wend structure in the macro of this typ reducing it's size to less than half and making it easier to understand.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2022 Sharon Parq Associates, Inc.
Comments