Solved

Excel macro to modify all tabs in a worksheet

Posted on 2014-12-11
7
112 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
ID: 40493856
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
ID: 40493889
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
ID: 40493893
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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40493895
Oops!
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40493912
That will teach me to answer a question without testing it first!
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 40494060
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
ID: 40494149
^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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

840 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