VBA Error

I have a VBA script to run.. Get an error 5 When I try to run..Upon debugging, I am told that the Line (I've underlined it) below is using the wrong argument... I've tried a few different ways to declare that worksheet as the one I need but to no avail.


Sub Tally()
Dim subj As String
subj = Worksheets("Bol Breakdown").Range("R1").Value
company = Worksheets("New").Range("N1").Value
filesformat = "PDF\Tally Sheets"
livescrap = Worksheets("New").Range("N3").Value
yeardate = Worksheets("New").Range("N4").Value
monthdate = Worksheets("New").Range("N5").Value
pathtosaveto = "S:\ARC\" & company & "\" & filesformat & "\" & yeardate & "\" & monthdate & "\" & subj

Worksheets("BOL Breakdown").ExportAsFixedFormat Type:=xlTypePDF, Filename:=pathtosaveto, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Well I don't know what is in your "pathtosaveto" but otherwise your code works for me. If the sheet name were incorrect you'd get a Subscript out of range error.
jmacalpineAuthor Commented:
pathtosaveto = "S:\ARC\" & company & "\" & filesformat & "\" & yeardate & "\" & monthdate & "\" & subj
Martin LissOlder than dirtCommented:
Here is the code I used. Try Dimming all your variables. When I ran it the first time it told me the PDF couldn't be created and I assume that's because I don't have that path. When I changed line 15 it worked fine.

Dim subj As String
Dim company As String
Dim filesformat As String
Dim yeardate As String
Dim monthdate As String
Dim pathtosaveto As String

subj = "Test" 'Worksheets("Bol Breakdown").Range("R1").Value
company = "ML Inc." 'Worksheets("New").Range("N1").Value
filesformat = "PDF\Tally Sheets"
'livescrap = Worksheets("New").Range("N3").Value
yeardate = "2015" 'Worksheets("New").Range("N4").Value
monthdate = "June" 'Worksheets("New").Range("N5").Value
'pathtosaveto = "C:\temp\" & company & "\" & filesformat & "\" & yeardate & "\" & monthdate & "\" & subj
pathtosaveto = "C:\temp\test"
Worksheets("Sheet1").ExportAsFixedFormat Type:=xlTypePDF, Filename:=pathtosaveto, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
error 5 is permission denied. you probably want to create the folder(s) you are trying to write to before actually trying to write to them.
on error resume next
MkDir "S:\ARC\" & company 
MkDir "S:\ARC\" & company & "\" & filesformat 
MkDir "S:\ARC\" & company & "\" & filesformat & "\" & yeardate 
MkDir "S:\ARC\" & company & "\" & filesformat & "\" & yeardate & "\" & monthdate 
On Error GoTo 0

Open in new window

the comnpany and such should not contain characters that cannot be used in a directory name. this code assumes the S:\ARC directory exists
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.