I have a workbook with three sheets. RC,RI,RCB. I need To write VBA code, which will copy ranges from this workbook and will paste to a newly created workbook.
In this code, everything Works except sheet RCB. Debug turns on on line 12
Sub Budget()
With Workbooks.Add
ActiveSheet.Name = "RC"
ThisWorkbook.Worksheets("RC").Range("A:D").Copy .Worksheets("RC").Range("A1")
ThisWorkbook.Worksheets("RC").Range("E:G").Copy .Worksheets("RC").Range("E1")
Sheets.Add
ActiveSheet.Name = "RI"
ThisWorkbook.Worksheets("RI").Range("A:E").Copy .Worksheets("RI").Range("A1")
ThisWorkbook.Worksheets("RI").Range("G:G").Copy .Worksheets("RI").Range("G1")
Sheets.Add
ActiveSheet.Name = "RCB"
ThisWorkbook.Worksheets("RCB").Range("A:C").Copy .Worksheets("RCB").Range("A1")
ThisWorkbook.Worksheets("RCB").Range("E:E").Copy .Worksheets("RCB").Range("G1")
.SaveAs "C:\Users\lomid\Desktop\4finansi3"
.Close
End With
End Sub
I think it has to be about active sheet and workbook. this is an error it gives me: "Run-time error 9: subscript out of range"
ThisWorkbook
contains a sheetRCB
? – FunThomasThisWorkbook
-sheets to sheets created in a new Workbook.Workbooks.Add
not only creates a new workbook, but sets it active. The code could be written (much) better, but this is not the issue. I did a quick test and it worked for me - assuming that the original workbook has all three sheets. – FunThomas