run-time error '1004' in VBA

learning_grexcel

Active Member
Joined
Jan 29, 2011
Messages
319
When I run the below code, I get the subject error.

However, when I remove this line from the code - "oldbook.Worksheets("prc").Range("A1").Select" , the code works fine. I'm not sure what the problem is with this line.

Code:
Set oldbook = ActiveWorkbook
Set newbook = Workbooks.Add
newbook.Worksheets("sheet1").Name = "org"
oldbook.Worksheets("sheet1").Range("A:A").Copy
newbook.Worksheets("org").Range("A1").PasteSpecial xlPasteValues
oldbook.Worksheets("sheet1").Range("C:C").Copy
newbook.Worksheets("org").Range("B1").PasteSpecial xlPasteValues
newbook.SaveAs Filename:="org2014.xls"
newbook.Worksheets("org").Range("A1").Select


oldbook.SaveAs Filename:="prc2014.xls"
oldbook.Worksheets("sheet1").Name = "prc"
oldbook.Worksheets("prc").Range("A:B").Copy
oldbook.Worksheets("prc").Range("D:E").PasteSpecial (xlPasteValues)
oldbook.Worksheets("prc").Range("A:B").Delete
oldbook.Worksheets("prc").Range("A1").Select
Application.CutCopyMode = False
oldbook.Worksheets("prc").Rows(1).Delete
oldbook.Worksheets("sheet2").Delete
oldbook.Worksheets("sheet3").Delete
newbook.Close savechanges:=True
oldbook.Close savechanges:=True
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try Activating the sheet before select statement




oldbook.Worksheets("prc").Activate
oldbook.Worksheets("prc").Range("A1").Select
 
Last edited:
Upvote 0
Thanks

Now I don't get the error, but it still doesn't have the cell A1 selected when I open the closed files after running the macro.
 
Upvote 0
I can't see any reason to select the cell. Try this
Code:
Option Explicit
Sub x()
    Dim newbook As Workbook
    With Application
        .ScreenUpdating = False
        On Error GoTo exit_Proc
        Set newbook = Workbooks.Add


        With newbook
            .Worksheets("sheet1").Name = "org"
            ThisWorkbook.Worksheets("sheet1").Range("A:A").Copy .Worksheets("org").Range("A1")
            ThisWorkbook.Worksheets("sheet1").Range("C:C").Copy .Worksheets("org").Range("B1")
            .Worksheets("sheet1").UsedRange.Value = .Worksheets("sheet1").UsedRange.Value
            .SaveAs Filename:="org2014.xls"
            .Close savechanges:=True
        End With


        With ThisWorkbook
            .SaveAs Filename:="prc2014.xls"
            .Worksheets("sheet1").Name = "prc"
            .Worksheets("prc").Range("A:B").Copy
            .Worksheets("prc").Range("D:E").PasteSpecial (xlPasteValues)
            .Worksheets("prc").Range("A:B").Delete
            .Worksheets("prc").Rows(1).Delete
            .Worksheets("sheet2").Delete
            .Worksheets("sheet3").Delete


            .Close savechanges:=True
        End With
exit_Proc:
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
End Sub
 
Upvote 0
Thanks Kris and Roy
@Kris, it was already above the close statement only.

@Roy, your code gives error at this line - .Worksheets("sheet1").UsedRange.Value = .Worksheets("sheet1").UsedRange.Value
 
Upvote 0

Forum statistics

Threads
1,213,507
Messages
6,114,029
Members
448,543
Latest member
MartinLarkin

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