Solved

Excel macro to modify all tabs in a worksheet

Posted on 2014-12-11
7
110 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 32

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Manipulation Macro 28 36
Struggling to Lock and grey out cells using VBA. Help please! 8 23
Hard coding time and date into Excel 2 30
Filling Blank Cells 14 10
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,…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

932 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

17 Experts available now in Live!

Get 1:1 Help Now