copying a range to different file

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I want to copy a range to another file. I wrote the code below. I can copy a range to same sheet, different sheets but not to different file. The code below did not give me an error message but it wont copy anything. I wrote similar code to copy one cell only and it did work. What is wrong with the code below. I appreciate your help. Thanks.

Code:
'working with range
    Sub myrange()
    Application.DisplayAlerts = False
    Dim x As Integer, y As Integer, i As Integer, j As Integer, k As Integer
    x = InputBox("row1")
    y = InputBox("col1")
    i = InputBox("row2")
    j = InputBox("col2")
    k = InputBox("color")
    Range(Cells(x, y), Cells(i, j)).Value = 100
    Range(Cells(x, y), Cells(i, j)).Interior.ColorIndex = k
    'copy range to same sheet
    Range(Cells(x, y), Cells(i, j)).Copy Cells(10, 10)
    'copy to different sheet
    Range(Cells(x, y), Cells(i, j)).Copy Sheets(2).Cells(10, 10)
    'copy to different file
    Dim wb As Workbook
    Dim fn As String
    fn = InputBox("file name")
    Set wb = Workbooks.Open(fn)
    Range(Cells(x, y), Cells(i, j)).Copy wb.Sheets(1).Cells(5, 5)
    wb.Save
    wb.Close
    Application.DisplayAlerts = True
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
When you open another workbook that will be the active workbook.
As you haven't qualified the range to copy, it will copy from the active sheet in the workbook you just opened.
 
Upvote 0
Thank you. I see. I changed that but now i am getting "application define or object define error"!

Code:
'copy to different file
    Dim wb As Workbook
    Dim fn As String
    fn = InputBox("file name")
    Set wb = Workbooks.Open(fn)
    ThisWorkbook.Worksheets(1).Range(Cells(x, y), Cells(i, j)).Copy wb.Worksheets(1).Cells(5, 5)
    wb.Save
    wb.Close
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
You need to qualify the cells as well.
 
Upvote 0
Thank you that resolved the issue. I never thought about it. Just another question please. I have Form Control Button in the first file. After trying to run that code several times, somehow I see the Control button in the sheet 1 of the second file! I thought Form Control buttons are not part of cells. How it gets copied to the second file! Thank you.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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