Prompt User to Select Sheet to Copy

sunstache32

New Member
Joined
Aug 17, 2015
Messages
5
As part of a larger macro, I have a working piece of code that opens a new workbook, copies the first sheet of that workbook, pastes the sheet into a new sheet of the original workbook, then closes the opened workbook. I want to change this to allow the user to choose which sheet to copy (instead of just the first). Is it possible to do this? Here is the code that copies the first sheet:

Code:
Dim wb_mainFile As Workbook
Dim strMainFile As String

strMainFile = Range("G4").value

'G4 is the cell that has the path for the workbook to open

Set wb_mainFile = Workbooks.Open(strMainFile)

ThisWorkbook.Activate
wb_mainFile.Sheets(1).Copy _

After:=wb3.Sheets(wb3.Sheets.Count)

ActiveSheet.Name = "Sheet3"
wb_mainFile.Close
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
How about:
Code:
Dim wb_mainFile As Workbook
Dim strMainFile As String
Dim x As Integer, str1 As String


    strMainFile = Range("G4").Value


'G4 is the cell that has the path for the workbook to open


    Set wb_mainFile = Workbooks.Open(strMainFile)


    str1 = "Choose the number of a sheet:" & Chr(10) & Chr(10)
    
    ctr = 1
    For Each sh In wb_mainFile.Worksheets
        str1 = str1 & ctr & "    " & sh.Name & Chr(10)
        ctr = ctr + 1
    Next sh
    
    x = InputBox(str1, "Enter the sheet")
    If x <> "" And x < ctr Then
        ThisWorkbook.Activate
        wb_mainFile.Sheets(x).Copy After:=wb3.Sheets(wb3.Sheets.Count)
    End If


    ActiveSheet.Name = "Sheet3"
    wb_mainFile.Close
 
Upvote 0
It works up until the part where you enter the number of the sheet you want. After entering the sheet number, the workbook is opened, but it doesn't copy the desired sheet (or any other sheets) and a type mismatch error is returned.
 
Upvote 0
Sorry about that. I tested most of it, and added a few lines later. Try this:
Code:
Sub CopySheet()
Dim strMainFile As String
Dim x As Integer, xstr as string, str1 As String

    strMainFile = Range("G4").Value

'G4 is the cell that has the path for the workbook to open

    Set wb_mainFile = Workbooks.Open(strMainFile)

    str1 = "Choose the number of a sheet:" & Chr(10) & Chr(10)
    
    ctr = 1
    For Each sh In wb_mainFile.Worksheets
        str1 = str1 & ctr & ".    " & sh.Name & Chr(10)
        ctr = ctr + 1
    Next sh
    
    xstr = InputBox(str1, "Enter the sheet")
    On Error goto BadNumber:
    x = CInt(xstr)

    If x > 0 And x < ctr Then
        ThisWorkbook.Activate
        wb_mainFile.Sheets(x).Copy After:=wb3.Sheets(wb3.Sheets.Count)
    End If

    ActiveSheet.Name = "Sheet3"
    wb_mainFile.Close
    
    Exit Sub

BadNumber:
    MsgBox "That's not a valid number"
End Sub
 
Upvote 0
Hello sir @Eric W , I'm sorry to interrupt but I tried this code. and after I enter the sheet number, it said "that's not a valid number". But I already put the right sheet no.

Perhaps you can enlighten me a bit on this error?
 
Upvote 0
You should only get that error message if you enter a value that isn't a number. If you type the word "one", or use the letter "l" (lower case L), for a 1, or some other combination of character and numeric data.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top