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

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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