copy a sheet from one workbook to another

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I have 2 workbooks are open. Workbooks(1) and Workbooks(2). The code and the button to run the code are in workbooks(1). The code below does not work after I entered the following

x= 1
y = 1
z =2
m = 1

Code:
Sub ws_copy_to_different_wb()
    Dim x As Integer
    Dim y As Integer
    Dim z As Integer
    x = InputBox("workbook #")
    y = InputBox("worksheet #")
    z = InputBox("destination book")
    m = InputBox("after which sheet in destination")
    Workbooks(x).Worksheets(y).Copy after:=Workbooks(z).Worksheets(m)
End Sub

Any idea why? thank you very much.
 
Last edited:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
What happens if you declare m as an integer, like you have the others?
 
Upvote 0
Try this

Code:
Sub ws_copy_to_different_wb()
    Dim x As Integer, y As Integer
    Dim z As Integer, m As Integer
    x = [COLOR=#0000ff]Application[/COLOR].InputBox("workbook #", , , , , , , [COLOR=#0000ff]1[/COLOR])  '[COLOR=#0000ff]Type number[/COLOR]
    y = Application.InputBox("worksheet #", , , , , , , 1)
    z = Application.InputBox("destination book #", , , , , , , 1)
    m = Application.InputBox("after which sheet in destination #", , , , , , , 1)
    Workbooks(x).Worksheets(y).Copy after:=Workbooks(z).Worksheets(m)
End Sub

But you have to verify how many books you have open.
Review the file names with the following so you can see which number corresponds to each book.

Code:
Sub test()
  Dim w As Object, n As Long
  n = 1
  For Each w In Workbooks
    MsgBox w.Name & " Number Book : " & n
    n = n + 1
  Next
End Sub
 
Upvote 0
Thank you all for this great help. Sorry I missed that. I only opened 2 workbooks. m was not declared as int. Thanks again
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,563
Messages
6,114,332
Members
448,566
Latest member
Nickdozaj

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