Solved

Excel macro to modify all tabs in a worksheet

Posted on 2014-12-11
7
109 Views
Last Modified: 2014-12-11
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.
0
Comment
Question by:dbfromnewjersey
7 Comments
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 250 total points
Comment Utility
Use:

for each sht in activeworkbook.sheets()
    Add your code here, with sht. instead of activesheet or sheets(1)
Loop

Open in new window

0
 
LVL 29

Expert Comment

by:leonstryker
Comment Utility
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

0
 
LVL 29

Accepted Solution

by:
leonstryker earned 250 total points
Comment Utility
LOL
    For Each sht In ActiveWorkbook.Worksheets()
        'Add your code here, with sht. instead of activesheet or sheets(1)
    Next

Open in new window

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
Oops!
0
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
That will teach me to answer a question without testing it first!
0
 
LVL 31

Expert Comment

by:Rob Henson
Comment Utility
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
0
 
LVL 27

Expert Comment

by:Glenn Ray
Comment Utility
^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
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now