+ Reply to Thread
Results 1 to 2 of 2

Insert sheet with macro

Hybrid View

  1. #1
    Registered User
    Join Date
    08-11-2015
    Location
    Scandinavia
    MS-Off Ver
    2010
    Posts
    1

    Question Insert sheet with macro

    Hi,

    I want a macro that:

    1. Inserts a new sheet based on a template (not an empty sheet)
    2. A box asking for the new sheet name I want to use, eg. ABCD
    3. If the sheet name already is used, it should count the sheets, eg. ABCD(1), ABCD(2) and so on.


    I have tried to combine information from several examples into this:
    Sub Insert_Sheet()
        Dim sh As Worksheet
        Dim shName As String
    
        'name of the sheet template
        shName = "J:\copy_sheet_mall.xltm"
    
        'Insert sheet template
        With ThisWorkbook
            Set sh = Sheets.Add(Type:=Application.TemplatesPath & shName, _
                                after:=.Sheets(.Sheets.Count))
        End With
    
        On Error Resume Next
         sh.Name = InputBox("Enter sheet name")
             If Err.Number > 0 Then
            MsgBox "Sheet name: " & sh.Name & " alredy in use."
            MsgBox "Sheet renamed to: " & sh.Name & Sheets.Count & ""
             Err.Clear
        End If
        On Error GoTo 0
    End Sub

    Problem 1:
    It seems like the template file must be in the Users\Appdata..... folder. Not possible with Network Access to J:\. Then it's not possible for multiple users to insert sheets based on the template. Any solution?
    Maybe an empty template within this files that it copies, instead of adding a new from a separate file?

    Problem 2:
    If the sheet name ABCD is being used, it renames it to Sheet1(2), Sheet1(3) and so on, instead of ABCD(2). How to solve this?

    Problem 3:

    Not the biggest issue, but it doesn't refer to the correct sheet names in the message boxes.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Insert sheet with macro

    Try this...
    Sub Insert_Sheet()
            
        Dim strName As String
        Dim i As Long
        Const shTemplate As String = "J:\copy_sheet_mall.xltm" 'Path and file name of the sheet template
        
        strName = Application.InputBox("Enter sheet name", "Sheet Name", Type:=2)
        If strName = "False" Then Exit Sub  'User canceled
        
        If Evaluate("ISREF('" & strName & "'!A1)") Then  'Test if worksheet name exists
            i = 2
            Do Until Not Evaluate("ISREF('" & strName & "(" & i & ")'!A1)")
                i = i + 1
            Loop
            MsgBox "Sheet name: " & strName & " alredy in use." & vbLf & vbLf & _
                   "Sheet renamed to: " & strName & "(" & i & ")", _
                   vbInformation, "Sheet Name Exists"
            strName = strName & "(" & i & ")"
        End If
        
        'Insert sheet template
        Sheets.Add(Type:=shTemplate, After:=Sheets(Sheets.Count)).Name = strName
        
    End Sub
    Last edited by AlphaFrog; 08-11-2015 at 07:39 AM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Protect sheet but allow insert macro.
    By r2fro in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-26-2014, 05:44 PM
  2. Macro for copying the data from one sheet and insert the same in next sheet
    By Thinker8 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-02-2013, 05:51 PM
  3. Macro to insert a formula to another sheet
    By Drayloc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-06-2012, 12:48 PM
  4. Macro to search sheet 1 for value and insert entire row from sheet 2
    By mtlong88 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-09-2011, 01:21 PM
  5. Insert Data From One Sheet to Another with Macro
    By niladri20005 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-30-2011, 08:14 AM
  6. macro tp insert excel sheet
    By navinaaaaaa in forum Excel General
    Replies: 1
    Last Post: 12-30-2010, 10:29 PM
  7. Macro - Combine/Insert Sheet
    By dugong in forum Excel General
    Replies: 2
    Last Post: 02-07-2007, 10:58 AM
  8. MAcro to copy and insert a sheet
    By JackR in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-19-2006, 09:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1