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
 

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

KrisSquare

Board Regular
Joined
Feb 1, 2014
Messages
66
Try Activating the sheet before select statement




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

learning_grexcel

Active Member
Joined
Jan 29, 2011
Messages
319
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.
 

royUK

Well-known Member
Joined
Jul 24, 2002
Messages
1,606

ADVERTISEMENT

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
 

learning_grexcel

Active Member
Joined
Jan 29, 2011
Messages
319
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
 

Teeroy

Well-known Member
Joined
Nov 9, 2012
Messages
2,244
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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

The offending line should be moved to the second With Block.
 

royUK

Well-known Member
Joined
Jul 24, 2002
Messages
1,606
Sorry looks like a typo

Remove the . from the front of that line
 

Watch MrExcel Video

Forum statistics

Threads
1,122,805
Messages
5,598,173
Members
414,217
Latest member
Mystix

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
Top