Sheets.add Type:= ?

roemun

New Member
Joined
Aug 6, 2009
Messages
23
What is the correct method to duplicate the last sheet when using Sheets.add Type: (how to finish this statement to have the newsheet be the same as the preceding sheet)
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
To duplicate a sheet:

Code:
Sheets("SheetName").Copy After:Sheets("SheetName")

VBA help has more info about the After and Before arguments of the Sheet.Copy method.
 
Upvote 0
The code I posted goes inside a macro.

"I need to do this in the form ..." sounds like homework.
If so, the VBEditor Help explains the use of the Add Method for worksheets.

If its not homework, why do you need to use the .Add method rather than .Copy?
 
Upvote 0
Thanks, again.
It is an assignment. I have looked in the MSDN site for an answer, but the only one I can find is for a file in another workbook, with the file path after Type:. But I thought there must be a more efficient way to accomplish this if I want to duplicate the preceding sheet in the active wbk, while still using the Sheets.Add method.
 
Upvote 0
Excel help says:

expression.Add(Before, After, Count, Type)

expression Required. An expression that returns one of the above objects.
Before Optional Variant. An object that specifies the sheet before which the new sheet is added.
After Optional Variant. An object that specifies the sheet after which the new sheet is added.
Count Optional Variant. The number of sheets to be added. The default value is one.
Type Optional Variant. Specifies the sheet type. Can be one of the following XlSheetType constants: xlWorksheet, xlChart, xlExcel4MacroSheet, or xlExcel4IntlMacroSheet. The default value is xlWorksheet.

so

Code:
sheets.Add type:=sheets(sheets.count).type

adds a sheet of the same type as the last sheet in the book and

Code:
sheets.Add type:=activesheet.type

adds a sheet of the same type as the currently selected sheet.

I'd be surprised if the macro recorder would give you this syntax, but I have to say I didn't know any of this until I read the help file. **** useful.
 
Upvote 0
Thanks, but those two sheets.add statements only insert a xlsheet without any formatting. I was hoping to find a add statement that would duplicate the last, or the active sheet.
 
Upvote 0
If you want to duplicate a sheet use Copy, type refers to a generic type of worksheet, not one you've created.

If you wanted you could create a template worksheet with all the formatting but not the data and copy that.
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,582
Members
449,039
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top