Solved

Excel macro to modify all tabs in a worksheet

Posted on 2014-12-11
7
115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

737 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