Copy Paste error message

22strider

Active Member
Joined
Jun 11, 2007
Messages
311
Hello Friends

Could you please check why am I getting error message "Script out of Range" for the following code for copying and pasting the data to another workbook?

Code:
Sub CreateWorkBook()

    Dim i As Long
    Dim strPath As String
    
    strPath = "C:\Rajesh\"
        
        For i = 1 To 10
        
      [Color=blue]Workbooks.Add.SaveAs (strPath & "Load" & i) [/Color]
            
            ThisWorkbook.Sheets("DataSheet").Range("A1,G20").Copy Workbooks(strPath & "Load" & i & ".xlsx") _
            .Sheets(1).Range("A1")
                        
        Next i

End Sub

The code works upto the line highlighted in blue and then gives the error message.
I am using this code in another bigger code.

Thanks for your help
Rajesh
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this.
Code:
Sub CreateWorkBook()
    Dim wbNew As Workbook
    
    Dim i As Long
    Dim strPath As String
    
    strPath = "C:\Rajesh\"
        
        For i = 1 To 10
        Set wbNew = Workbooks.Add
        
        wbNew.SaveAs strPath & "Load" & i
            
            ThisWorkbook.Sheets("DataSheet").Range("A1:G20").Copy wbNew.Sheets(1).Range("A1")
            
                        
        Next i
End Sub
Note I've changed the range you are copying - you would get an error with Range("A1, G20").

If you do want to copy just cells A1 and G20 then you would need to alter the code further.
 
Upvote 0

Forum statistics

Threads
1,211,839
Messages
6,104,296
Members
447,901
Latest member
boy3hc2004

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