lezawang
Well-known Member
- Joined
- Mar 27, 2016
- Messages
- 1,805
- Office Version
- 2016
- Platform
- 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.
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