Consulting

Results 1 to 8 of 8

Thread: Inserting worksheets automatically in the same workbook..Guidance reqd

  1. #1
    VBAX Newbie
    Joined
    Jun 2007
    Posts
    4
    Location

    Wink Inserting worksheets automatically in the same workbook..Guidance reqd

    Hello Everybody

    I have a named range (cname) which contains names like
    abc
    def
    ghi
    jkl
    mno
    pqr
    stu...
    .... and more..
    I want to automatically create worksheets with the formulas and format as in the first sheet, in the same workbook with the above names like sheet1 will be named as abc, sheet2 as def, sheet3 as ghi... so on

    If i add any name, a new sheet should be inserted in the workbook while checking for duplication. No duplicate sheet for the same name should be inserted. The sheet name could be more than 3 characters

    Can anybody tell me how to go about this..
    any macros available?

    Thanks a ton in advance
    Rajesh

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Worksheet_Change(ByVal Target As Range)
    Const WS_RANGE As String = "H1:H10" '<== change to suit
    Const SHEET_2COPY As String = "Master" '<== change to suit
    Dim sh As Worksheet

    On Error GoTo ws_exit
    Application.EnableEvents = False

    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
    With Target
    On Error Resume Next
    Set sh = Worksheets(.Value)
    On Error GoTo 0
    If sh Is Nothing Then
    Worksheets(SHEET_2COPY).Copy after:=Worksheets(Worksheets.Count)
    ActiveSheet.Name = .Value
    End If
    On Error GoTo 0
    Me.Activate
    End With
    End If

    ws_exit:
    Application.EnableEvents = True
    End Sub
    [/vba]

    This is worksheet event code, which means that it needs to be
    placed in the appropriate worksheet code module, not a standard
    code module. To do this, right-click on the sheet tab, select
    the View Code option from the menu, and paste the code in.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Newbie
    Joined
    Jun 2007
    Posts
    4
    Location

    It doesnt work

    Quote Originally Posted by xld
    [vba]

    Private Sub Worksheet_Change(ByVal Target As Range)
    Const WS_RANGE As String = "H1:H10" '<== change to suit
    Const SHEET_2COPY As String = "Master" '<== change to suit
    Dim sh As Worksheet

    On Error GoTo ws_exit
    Application.EnableEvents = False

    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
    With Target
    On Error Resume Next
    Set sh = Worksheets(.Value)
    On Error GoTo 0
    If sh Is Nothing Then
    Worksheets(SHEET_2COPY).Copy after:=Worksheets(Worksheets.Count)
    ActiveSheet.Name = .Value
    End If
    On Error GoTo 0
    Me.Activate
    End With
    End If

    ws_exit:
    Application.EnableEvents = True
    End Sub
    [/vba]

    This is worksheet event code, which means that it needs to be
    placed in the appropriate worksheet code module, not a standard
    code module. To do this, right-click on the sheet tab, select
    the View Code option from the menu, and paste the code in.
    Thanks for the input
    But this doesnt seem to work
    there is no change taking place
    Pls help...

    Thanks
    Rajesh

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You need a template sheet called Master, and to type something into one of H1:H10.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Newbie
    Joined
    Jun 2007
    Posts
    4
    Location

    Smile

    Quote Originally Posted by xld
    You need a template sheet called Master, and to type something into one of H1:H10.
    Hi
    I have a template called Master
    I tried the same but a new sheet Master(2) is only being created

    It is not being named as i want and only one sheet is being created

    Thanks
    Rajesh
    Last edited by shrejeet; 07-06-2007 at 06:59 AM. Reason: file attached

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Your example workbook works fine for me. Just re-edit the names to get them retrospectively.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Newbie
    Joined
    Jun 2007
    Posts
    4
    Location
    Quote Originally Posted by xld
    Your example workbook works fine for me. Just re-edit the names to get them retrospectively.
    Unfortunately it doesnt seem to work here.. I am using excel 2007
    Do i have to run the code every time ?
    Help will be highly appreciated
    Can the file be attached??
    Rajesh

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Works fine for me in 2007 also.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •