+ Reply to Thread
Results 1 to 7 of 7

Creating new sheet based on user input - old sheets get deleted by mistake

Hybrid View

  1. #1
    Registered User
    Join Date
    03-06-2024
    Location
    London, England
    MS-Off Ver
    Microsoft 365 for Enterprise
    Posts
    3

    Creating new sheet based on user input - old sheets get deleted by mistake

    Hi all,

    I'm currently trying to use a user input to name a duplicate sheet based off a 'template sheet' within the same workbook, then populate cell B11 of the duplicate sheet with the user's input. This seemed to work fine, however, after doing 4 duplicate sheets, some of the new sheets get deleted/renamed and hidden? not sure where i'm going wrong here. Any help would be greatly appreciated! Let me know if you need more information. Thanks




    Sub AdditionalItems()
      
      Dim newName As String
      Dim ts As Worksheet
      Dim ws As Worksheet
      Dim wb As Workbook
      Dim nss As Worksheet
      Dim lastow As Long
      Set wb = ThisWorkbook
      
      With wb
    Application.ScreenUpdating = False
    
        Set ws = Sheets("DATABASE")
        Set ts = Sheets("STUDY TEMPLATE 1")
        lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
        ts.Visible = xlSheetVisible 'shows template - can only create new sheets if template is visible
    
      On Error Resume Next
      
      newName = InputBox("Enter the name for the copied worksheet", "New Project") 'user input for name of new sheet
        ts.Visible = xlSheetHidden
      If newName <> "" Then
        ts.Copy After:=Worksheets(Sheets.Count)
        ts.Visible = xlSheetHidden
        
        On Error Resume Next
        
        Set nss = ActiveSheet
        nss.Name = newName 'changes name to user input
        nss.Range("B11").Value = newName 'adds name to newly created sheet
        
        ws.Cells(lastrow, "A").Value = newName 'adds newly created sheet to database
        ns.Range("B11").Clear
    
        ts.Visible = xlSheetHidden
        Application.ScreenUpdating = True
           
      End If
      End With
    End Sub

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Creating new sheet based on user input - old sheets get deleted by mistake

    Hi there,

    Without sight of your workbook it's difficult to know exactly what is/is not happening.

    
      If newName <> "" Then
        ts.Copy After:=Worksheets(Sheets.Count)
        ts.Visible = xlSheetHidden
        
        On Error Resume Next
        
        Set nss = ActiveSheet
        nss.Name = newName 'changes name to user input
    When you hide a worksheet, you don't have full control over which worksheet then becomes the active worksheet. In most cases the NEXT (i.e. the next tab on the right-hand side) worksheet becomes active, however if you hide the last (i.e. rightmost) worksheet, the PREVIOUS worksheet becomes active. Something like this might help to explain any inconsistent results you're experiencing.

    Hope this helps.

    Regards,

    Greg M

  3. #3
    Registered User
    Join Date
    03-06-2024
    Location
    London, England
    MS-Off Ver
    Microsoft 365 for Enterprise
    Posts
    3

    Re: Creating new sheet based on user input - old sheets get deleted by mistake

    Hi Greg,

    Really appreciate the response! Thanks

    I've attached an example copy of the spreadsheet I'm working on. There's two template versions, one I've hidden the template for and removed the visible = xlSheetHidden lines (Template 1) and the other I haven't (Template 2). I hope this helps clarify what is/is not happening. How would I be able to reference the newly created sheet within the code? Ideally I would like the template sheets to be hidden, but not sure if this is possible without using sheet indexing.
    Attached Files Attached Files
    Last edited by dylanherri97; 03-06-2024 at 01:24 PM.

  4. #4
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Creating new sheet based on user input - old sheets get deleted by mistake

    Possibly...
    Sub AdditionalItems_2()
        Dim newName As String
    
        Application.ScreenUpdating = False
        newName = InputBox("Enter the name for the copied worksheet", "New Project")
        If Not newName = vbNullString Then
            With Sheets("STUDY TEMPLATE 1")
                .Visible = xlSheetVisible
                .Copy After:=Sheets(Sheets.Count)
                .Visible = xlSheetHidden
            End With
            Sheets(Sheets.Count).Name = newName
            Sheets("DATABASE").Cells(Rows.Count, 1).End(xlUp).Offset(1) = newName
        End If
        Application.ScreenUpdating = True
    End Sub

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Creating new sheet based on user input - old sheets get deleted by mistake

    Hi again,

    Unfortunately there are several basic errors in the code you posted originally, e.g.:

    
    Sub AdditionalItems()
      
      Dim newName As String
      Dim ts As Worksheet
      Dim ws As Worksheet
      Dim wb As Workbook
      Dim nss As Worksheet
      Dim lastow As Long
      Set wb = ThisWorkbook
      
      With wb
    Application.ScreenUpdating = False
    
        Set ws = Sheets("DATABASE")
        Set ts = Sheets("STUDY TEMPLATE 1")
        lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
        ts.Visible = xlSheetVisible 'shows template - can only create new sheets if template is visible
    
      On Error Resume Next
      
      newName = InputBox("Enter the name for the copied worksheet", "New Project") 'user input for name of new sheet
        ts.Visible = xlSheetHidden
      If newName <> "" Then
        ts.Copy After:=Worksheets(Sheets.Count)
        ts.Visible = xlSheetHidden
        
        On Error Resume Next
        
        Set nss = ActiveSheet
        nss.Name = newName 'changes name to user input
        nss.Range("B11").Value = newName 'adds name to newly created sheet
        
        ws.Cells(lastrow, "A").Value = newName 'adds newly created sheet to database
        ns.Range("B11").Clear
    
        ts.Visible = xlSheetHidden
        Application.ScreenUpdating = True
           
      End If
      End With
    End Sub
    The compiler would have identified these errors if you had placed "Option Explicit" as the first line in the CodeModule - you should ALWAYS do this.

    Also, you should not include open-ended "On Error Resume Next" statements - these can produce VERY strange/dangerous results! If you need to use them, you should always restore error handling as soon as possible by means of an "On Error GoTo 0" statement.


    See if the attached version of your workbook does what you need - it uses the following code:

    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub AdditionalItems()
    
        Call CreateNewWorksheet(sSourceSheetName:="STUDY TEMPLATE 1")
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub PerPatient_Click()
     
        Call CreateNewWorksheet(sSourceSheetName:="STUDY TEMPLATE 2")
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub CreateNewWorksheet(sSourceSheetName As String)
      
        Dim wksTemplate     As Worksheet
        Dim wksDatabase     As Worksheet
        Dim lLastRowNo      As Long
        Dim sNewName        As String
        Dim wksNew          As Worksheet
    
        sNewName = InputBox("Enter the name for the copied worksheet", "New Project")
    
        If sNewName <> "" Then
    
            Set wksDatabase = Sheets("DATABASE")
            Set wksTemplate = Sheets(sSourceSheetName)
    
            With wksDatabase
                lLastRowNo = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            End With
    
            Application.ScreenUpdating = False
    
                wksTemplate.Copy After:=Worksheets(Worksheets.Count)
    
                Set wksNew = Worksheets(Worksheets.Count)
    
                With wksNew
    
                    .Visible = xlSheetVisible
                    .Name = sNewName
                    .Range("B11").Value = sNewName
                    .Activate
    
                End With
    
                wksDatabase.Cells(lLastRowNo, "A").Value = sNewName
    
            Application.ScreenUpdating = True
    
      End If
    
    End Sub
    Worksheets CAN be copied (as hidden worksheets) when their Visible property is set to "Hidden" - it's only when that property is set to "VeryHidden" that they can't be copied.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files
    Last edited by Greg M; 03-06-2024 at 09:18 PM. Reason: Extra highlighting added

  6. #6
    Registered User
    Join Date
    03-06-2024
    Location
    London, England
    MS-Off Ver
    Microsoft 365 for Enterprise
    Posts
    3

    Re: Creating new sheet based on user input - old sheets get deleted by mistake

    Hi Greg,

    Thank you very much and thanks for explaining some of the more egregious errors in my original code, it makes a lot of sense now!

    Your updated template seems to work perfectly and is incredibly easy to add new templates etc as well.

    Thanks again

  7. #7
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Creating new sheet based on user input - old sheets get deleted by mistake

    Hi again,

    Many thanks for your feedback.

    You're welcome - glad I was able to help.

    Regards,

    Greg M

+ 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. Creating an input sheet that outputs on to different sheets.
    By Sheamau5 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-15-2014, 08:05 AM
  2. Please Help with Copy/Paste and creating sheets depending on user input
    By rrstull in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-06-2013, 05:43 AM
  3. Creating varied templates based on user input when opened?
    By Set in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-22-2012, 05:46 PM
  4. Creating a spreadsheet based upon user data input
    By kriebelken in forum Excel General
    Replies: 0
    Last Post: 08-01-2012, 04:55 AM
  5. [SOLVED] Refreshing/Recalculating Sheets based on user input or switching between sheets
    By SonOfOdin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-24-2012, 09:48 PM
  6. Creating summation formula based on user input.
    By gshock in forum Excel General
    Replies: 2
    Last Post: 01-08-2009, 09:49 AM
  7. Creating Input box that grap data based on user answer
    By salooha in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-01-2006, 07:30 PM

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