Search code examples
excelvbauserform

I need a code to work with a userform. The code needs to copy sheet1 and name it according to two text boxes in the userform


The userform has two text boxes, one to enter the current month and another to enter the year (would be nice if the year is pre typed). It also has two buttons, one to create the new sheet and another to cancel. I would like a code to copy Sheet1 and name it according to the two text boxes in the userform. IE: May 2019. That code would have to copy four command buttons that exist in sheet1, along with the button locations, button codes and all the cells with information in them. These four command buttons are New entry, Update Entry, Delete Entry and New Log. (the New Log command button will have the code that i am asking for). Once the new sheet is created, the command buttons would have to work for the new sheet. Image of Sheet1 With UserForm Opened


Solution

  • You can use a hidden template sheet.Something like this:

    application.screenupdating = false
    
    with thisworkbook.sheets("template")
        .visible = true
        .copy after:=thisworkbook.sheets(thisworkbook.sheets.count)
        .visible=false
    end with
    
    thisworkbook.sheets(thisworkbook.sheets.count).name = SheetName
    
    application.screenupdating = true