+ Reply to Thread
Results 1 to 2 of 2

Duplicate worksheet question

  1. #1
    TimN
    Guest

    Duplicate worksheet question

    Similar to a question from yesterday, but a new twist.

    I have the following code which upon clicking the OK command button, the
    user is asked in a message box to enter a name for a new worksheet. The
    active worksheet is then copied to this new sheet and it is saved in the same
    workbook.


    Private Sub cmdOK_Click()
    Do
    Dim nSheet As Worksheet
    Dim NameBox As String
    NameBox = Application.InputBox("Please type a name for the new worksheet",
    "Creating New Sheet", , , , , , 2)
    If NameBox = "" Or NameBox = "False" Then
    MsgBox "Please type a name for the new worksheet"
    End If
    Loop Until Not NameBox = "" Or NameBox = "False"
    Sheets("STD Calc").Copy Before:=Sheets(2)
    Set nSheet = ActiveSheet
    nSheet.Name = NameBox
    Unload Me
    End Sub

    How can I tweak this code so that it doesn't copy to a brand new worksheet
    as named by the user, rather it copies to an existing worksheet within this
    same workbook? The user would just need to name which worksheet it is copied
    to.

    Thanks for any help!





  2. #2
    Registered User
    Join Date
    06-02-2006
    Posts
    39
    To make it easiest for the user, I would create a form with a list box and a command button. Then, you could load the list box with the sheet names and the user could select from the list. Something like this:

    Private Sub Userform_Initialize()
    Dim intsheets As Integer

    ListBox1.Clear

    intsheets = 2

    Do While intsheets < (Sheets.Count + 1)
    ListBox1.AddItem Worksheets(intsheets).Name
    intsheets = intsheets + 1
    Loop
    End Sub

    Private Sub cmdOK_Click()

    Dim nSheet As Worksheet
    Dim NameBox As String

    Namebox = Listbox1.Value

    Application.DisplayAlerts = False
    Worksheets(Namebox).Delete
    Application.DisplayAlerts = True

    Sheets("STD Calc").Copy Before:=Sheets(2)

    Set nSheet = ActiveSheet
    nSheet.Name = NameBox

    Unload Me
    End Sub

    This will delete the named sheet and then copy the specified sheet, renaming it to the old sheet.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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