简体   繁体   中英

Copying a worksheet in Excel 2013 using VBA to the end of the workbook

I am working in Excel 2013 and am having some trouble. I have researched online all day and can't seem to find anything that solves my problem.

I have an Excel document that consists of 6 sheets: Format Summary 1.1 Detail 1.2 Detail 1.3 Detail 1.4 Detail

The 'detail' sheets are formatted all the same. The 'Summary' sheet rolls up all the values from the 'details' sheets and has all kinds of charts and useful information in it. The 'format' sheet is what I created to try to make this document more dynamic and useful for all different kinds of projects. This document is being used for estimating projects, so different projects are going to have different amounts of 'details' sheets.

SO, my goal is to have a blank in the 'format' sheet that asks how many details are needed. When the user types in 5, the code will add a sheet named '1.5 Detail' to the end of the workbook and have it formatted the same as the other 'details' sheets. Is this possible using VBA? Also, is there a way to make the charts and formulas include the new tab when it is added without manually going and adding it to everything?

Thanks in advance!

Let's say you had three detail worksheets named: d_1, d_2, and d_3.

Each has an Excel Table named Labor with a column entitled "Cost". Each Labor table has a unique number of data rows.

On a summary sheet you could sum all of them like so:

=SUM(d_1:d_3!Labor[Cost])

The above will give you the labor cost grand total from all sheets. It is summing all of the rows from the tables and is not dependent on any summary information in the detail sheets.

So if you add a new detail sheet, d_4, then yes the example formula would need to be updated. HOWEVER, there is an old trick to get around this...

Create two additional sheets with the same tables, but with only one row of ZERO values in each table. Call the first one of these sheets "START". Call the second one of these sheets "END". Place START immediately before the first detail sheet, and place END immediately after the last detail sheet. NOW MAKE BOTH INVISIBLE.

Finally change the example formula to:

=SUM(START:END!Labor[Cost])

Now when new detail sheets are added, d_4, d_5, d_6, d_n, no change needs to happen to the formula on the summary sheet, yet all of the new data are included in the summary calculation.

Use ThisWorkbook.Worksheets.Count to count the number of sheets. Then if it is less, use Worksheets.Add and ActiveSheet.Move After:=Sheets(ActiveWorkbook.Sheets.Count) to add it on to the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM