Avatar of Jenkins
JenkinsFlag for United States of America

asked on 

Excel macro to modify all tabs in a worksheet

I routinely deal with Excel files that have multiple tabs.   The setup/format of each tab is the same.  For example, I might have an Excel file with a separate tab for each state of the union.   I need a reusable and easily modifiable macro that will allow me to change data in each tab of the file instead of having to go into each tab individually and make the change.  For example, let's say row 2, column 1 of each tab contains the current month.  When next month comes along, I'm currently forced to go into each tab individually to update the month. I need something that will let me change them all in one shot.

This macro needs to run from a Personal.XLSB file, which I already have, so that I can use it on any Excel file instead of having to insert the macro in every indivual Excel file I use.
Microsoft Excel

Avatar of undefined
Last Comment
Glenn Ray
SOLUTION
Avatar of Phillip Burton
Phillip Burton

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of leonstryker
leonstryker
Flag of United States of America image

One correction to Phillip's code is that you need to use Next instead of Loop in a For Each. You may also use the Worksheets collection instead of Sheets

    For Each sht In ActiveWorkbook.Worksheets()
        'Add your code here, with sht. instead of activesheet or sheets(1)
    Loop

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leonstryker
leonstryker
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Phillip Burton
Phillip Burton

Oops!
Avatar of Phillip Burton
Phillip Burton

That will teach me to answer a question without testing it first!
Avatar of Rob Henson
Rob Henson
Flag of United Kingdom of Great Britain and Northern Ireland image

Alternatively, if all the tabs have like in your example a cell with the date, have the date input on one tab and have all the other tabs linked to it by formula.

So for example in Cell B2 on Sheet1 enter the date. Sheet2 Cell B2 would then have the formula:  =Sheet1$B$2

Copy and paste it to other sheets. If all sheets are exactly the same ie the date in cell B2, copy from Sheet2 and then select Sheet3 cell B2, press shift and select the tab for the last sheet. Then do the Paste. This will paste into all selected sheets. Deselect all sheets by then selecting Sheet2 otherwise any amendments while multiple sheets are selected will affect all selected sheets.

Thanks
Rob H
Avatar of Glenn Ray
Glenn Ray
Flag of United States of America image

^This.  

I love automation, but this is one of those cases where a formula will suffice and actually work faster than a macro.

For example, if each sheet represents a sequential month (ex., cell A2 - first sheet = "January", second sheet = "February"... and so on), then you could do this:
1) In the first sheet, insert your date (ex., "12/1/2014") and format as appropriate.
2) In the second sheet, insert this formula:
=EOMONTH(firstsheetname!A2,0)+1
3) Repeat inserting this formula in subsequent sheets, but increment the months argument (,0) by one in each case
=EOMONTH(firstsheetname!A2,1)+1
=EOMONTH(firstsheetname!A2,2)+1
=EOMONTH(firstsheetname!A2,3)+1
...and so on.  firstsheetname should be replaced with the actual sheet tab name of the first sheet in the workbook. If it has spaces in it, you'll need to enclose in single quotes/apostrophes (ex., 'Month 1')

Whenever you change the date on the first sheet, all subsequent sheet dates will change immediately - no macro needed.  This will work across calendar years.

See the example workbook.

Regards,
-Glenn
EE-AutoMonths.xlsx
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo