Requirement:
The user is looking to click a button and name and create a new sheet and then copy, data from the sheet I just copied and paste it into the new sheet. Basically just create an exact copy of an existing page just be prompted to name the new sheet.
The following code will create a new page on a pop up but the user can't yet to figure out how to incorporate the copying and pasting.
Sub AddSheets() Dim NewName As String Another: OneMore = False NewName = InputBox("What Do you Want to Name the New Sheet?") Sheets.Add ActiveSheet.Name = NewName If MsgBox("Do you want to Add another Sheet?", vbYesNo) = 6 Then Goto Another End Sub
Solution:
Replace the commandbutton click event code with this...
Private Sub CommandButton1_Click() Dim SheetName As String Dim NewSheetName As String Dim sws As Worksheet, dws As Worksheet If Me.ListBox1.ListIndex = -1 Then MsgBox "Please select a Sheet to Copy.", vbExclamation, "Sheet Not Selected!" Exit Sub End If If Me.TextBox1 = "" Then MsgBox "Please input the New Sheet name.", vbExclamation, "New Sheet Name Missing!" Me.TextBox1.SetFocus Exit Sub End If SheetName = Me.ListBox1.List(Me.ListBox1.ListIndex) NewSheetName = Me.TextBox1.Value Set sws = Sheets(SheetName) sws.Visible = True Sheets(SheetName).Copy after:=Sheets(Sheets.Count) On Error Resume Next Set ws = ActiveSheet ws.Name = NewSheetName If Err <> 0 Then Application.DisplayAlerts = False ws.Delete MsgBox "Please input a valid Sheet Name", vbCritical, "Invalid Sheet Name!" Exit Sub End If sws.Visible = False '*********************************************** 'Here you can perform the desired manipulation on newly created sheet 'e.g. ws.Range("A1").Value = Now 'etc '*********************************************** Unload Me End Sub
The user wants to know how to remove the first sheet from the pop up list? So it only shows sheet 2,3 and 4?
Private Sub UserForm_Initialize() Dim ws As Worksheet For Each ws In Worksheets If ws.Index <> 1 Then Me.ListBox1.AddItem ws.Name End If Next ws End Sub
Obtained from the OzGrid Help Forum.
Solution provided by sktneer.
See also: Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions and Index to new resources and reference sheets
See also:
How to copy a sheet and rename from a list, ignore duplicates |
How to copy master sheet as values and automatically set new name |
How to re-name each sheet in workbook with a pre-defined name |
How to alternate row colours based on text name |
Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.