You are here:

Excel/Assistance with code

Advertisement


Question
Below code creates worksheets based on template on sheet2 and names them as per names in Column A on sheet1.

Sub AddSheet()
   Application.ScreenUpdating = False
   Dim bottomA As Integer
   bottomA = Range("A" & Rows.Count).End(xlUp).Row
   Dim c As Range
   Dim ws As Worksheet
   For Each c In Range("A1:A" & bottomA)
       Set ws = Nothing
       On Error Resume Next
       Set ws = Worksheets(c.Value)
       On Error GoTo 0
       If ws Is Nothing Then
         Sheets("Sheet2").Select
         Sheets("Sheet2").Copy After:=Sheets(Sheets.Count)
         ActiveSheet.Name = c.Value
       End If
   Next c
   Application.ScreenUpdating = True
End Sub

The problem is when I enter numbers 1,2,3 in column A the macro creates worksheets only for 3 and not for 1 and 2. Kindly Asssist.

Answer
Bimmy,

If you are literally doing
A1:  1
A2:  2
A3:  3


then I assume you have two sheets already in the workbook.  You code

set ws = Worksheets(c.value)

will be successful for 1 and 2 because  Worksheets(arg1)  allows an index number such as 1 or 2 or a name  such as "Sheet1", "Sheet2"

since you cells contains numbers,   Worksheets(1)   is the first worksheet in the tab order and Worksheets(2) is the second worksheet in the tab order.  So new worksheets are not created.

This modification will create worksheets named  "1", "2", "3"

Sub AddSheet()
   Application.ScreenUpdating = False
   Dim bottomA As Integer
   bottomA = Range("A" & Rows.Count).End(xlUp).Row
   Dim c As Range
   Dim ws As Worksheet
   Debug.Print Range("A1:A" & bottomA).Address(0, 0, xlA1, True)
   'Exit Sub
   For Each c In Range("A1:A" & bottomA)
       Set ws = Nothing
       On Error Resume Next
       Set ws = Worksheets(Trim(CStr(c.Value)))   '<== modified line
       On Error GoTo 0
       If ws Is Nothing Then
         Sheets("Sheet2").Select
         Sheets("Sheet2").Copy After:=Sheets(Sheets.Count)
         ActiveSheet.Name = c.Value
       End If
   Next c
   Application.ScreenUpdating = True
End Sub

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.