add worksheet
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Add a worksheet

Add a worksheet

resolvedResolved · Urgent Priority · Version 2007

replyReply Wed 30 Mar 2011, 12:36Delegate Tracey said...

Tracey has attended:
Excel VBA Intro Intermediate course

Add a worksheet

I was provided on the course with code to search for a sheet and delete it if it exists, and also to add a new sheet.

I am trying to amalgamate code to say

for each sheet, if the sheet is named x, clear contents otherwise create sheet named 'x', but it tries to create the sheet as soon as it hits another sheet.

How do I write the code to create a sheet named 'x' if it doesn't exist? (I can't delete existing sheets named 'x' as other sheets reference them).

For upcoming training course dates see: Pricing & availability

replyReply Mon 4 Apr 2011, 12:50Trainer Simon said...

RE: add a worksheet

Hi Tracey,

Thank you for your question.

I think the following code may help you as a good starting point as you would have the code to create a new worksheet anyway from the course.

The following routine checks the collection of sheets to say delete all sheets that is not equal to 'Total Sales' and 'List Page'.

You can try and adapt this:

Dim shtNames() As String ' variable

Application.DisplayAlerts = False
ReDim shtNames(1 To ActiveWorkbook.Sheets.Count) 'resizing array
For i = 1 To Sheets.Count
shtNames(i) = Sheets(i).Name
Next i
'array that contains all the sheet names within it

For i = 1 To Sheets.Count

If Sheets(shtNames(i)).Name <> "Total Sales" And Sheets(shtNames(i)).Name <> "List Page" Then

Sheets(shtNames(i)).Delete


Else

End If

Next i
Application.DisplayAlerts = True
MsgBox "All previous sheets have been deleted"

I hope this helps.

Regards

Simon

 

Please browse our web site to find out more about
excel training uk and other Microsoft training courses.

Excel tip:

Hiding Formulae in the Formula Bar

It is possible to protect the contents of a cell reference(s) from amendment by applying cell protection. The contents of the cell reference can also be stopped from displaying in the formula bar.

Step 1: Select Format > Cells > Protection.

Step 2: Tick Hidden option. Ensure Locked is ticked

Step 3: Select Tools > Protection > Protect Sheet

(Ensure "Protect worksheet and contents of locked cells" is ticked)


View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard