runtime error 1004

mellows23

New Member
Joined
Jan 19, 2018
Messages
4
Hi There,

I am getting a runtime error on the following?

It basically puts some data into a couple of tabs, then saves those two tabs as a pdf. Inputs new data and creates a pdf again in a loop. There are about 25 iterations of this required. But after somewhere between 5-7 I get a runtime error.

Any ideas?

Code below;



Private Sub CommandButton1_Click()

'copy and paste onto po sheet

Dim A As Integer
Dim B As Integer
Dim FolderPath As String

'capture no. of iterations

B = Sheet1.Range("iterations").Value

'copy and paste data

For A = 1 To B

Sheet3.Range("a8") = Sheet1.Range("G1").Offset(A, 0)
Sheet2.Range("b2") = Sheet1.Range("c1").Offset(A, 0)
Sheet2.Range("b3") = Sheet1.Range("H1").Offset(A, 0)
Sheet2.Range("b6") = Sheet1.Range("d1").Offset(A, 0)
Sheet2.Range("h1") = Sheet1.Range("e1").Offset(A, 0)
Sheet3.Range("a9") = Sheet1.Range("f1").Offset(A, 0)
Sheet3.Range("k29") = Sheet1.Range("I1").Offset(A, 0)

FolderPath = "C:\Users\amellows\Desktop"

Dir FolderPath

Sheets(Array("Project details", "cost breakdown")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FolderPath & Sheet1.Range("J1").Offset(A, 0), _
openafterpublish:=True, ignoreprintareas:=False

Next





End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Sheets(Array("Project details", "cost breakdown")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FolderPath & Sheet1.Range("J1").Offset(A, 0), _
openafterpublish:=True, ignoreprintareas:=False

I noticed you are opening each file after creation? perhaps the bolded filename you are saving as is the same and the file is open and therefore locked?
just a thought. if so, insert your variable 'A' into the filename, along with an undersoce.. e.g. Filename:=FolderPath & A & "_" & Sheet1.Range("J1").Offset(A, 0),
Also, I see FolderPath doesn't end with a backslash, does the value in Sheet1.Range("J1").Offset(A,0) start with a backslash?

HTH
 
Last edited:
Upvote 0
I noticed you are opening each file after creation? perhaps the bolded filename you are saving as is the same and the file is open and therefore locked?
just a thought. if so, insert your variable 'A' into the filename, along with an undersoce.. e.g. Filename:=FolderPath & A & "_" & Sheet1.Range("J1").Offset(A, 0),
Also, I see FolderPath doesn't end with a backslash, does the value in Sheet1.Range("J1").Offset(A,0) start with a backslash?

HTH


Seems to be working now, thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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