prevent add sheet if the sheet is already exits

wiwi
12-15-2003, 11:38 PM
I have a macro that will add sheets (costing,T&A,order confirmations) into activeworkbooks (marketing.xls). If the sheets (costing (2), T&A (2),order confirmations (2)) are already exits, then will prompt user a msg, if not will create the sheets for them.
but when I run the macro, there will created 8 set of the three sheets.
pls help...


For Each ws In ActiveWorkbook.Worksheets
If ws.Name Like "Costing (2)" Then
MsgBox "You must save your current costing before create another costing"
Else
Worksheets("Costing").Visible = True
Worksheets("T&A").Visible = True
Worksheets("Order Confirmations").Visible = True

Sheets(Array("Costing", "T&A", "Order Confirmations")).Select
Sheets("Order Confirmations").Activate
Sheets(Array("Costing", "T&A", "Order Confirmations")).Copy Before:=Workbooks( _
"Marketing.xls").Sheets(2)
End If
Next ws

Worksheets("Costing").Visible = xlVeryHidden
Worksheets("T&A").Visible = xlVeryHidden
Worksheets("Order Confirmations").Visible = xlVeryHidden

tinyjack
12-16-2003, 01:01 AM
This is becuase you are doing the copying inside your For Each loop.

It looks like the only purpose of this look is to see if a sheet called "Costing (2)" exists, so why not set a flag to false, run through your loop checking the sheet names and set flag to true if "Costing (2)" found, then outside of the loop check flag and either display message or perform action depending on the flag state.

TJ

wiwi
12-16-2003, 01:20 AM
Hi :chuckle:

Thanks for your suggestion..and it's work

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum