Receiving Error Message on VBA Code

findingcindy

New Member
Joined
Apr 3, 2025
Messages
7
Office Version
  1. 2013
Platform
  1. Windows
I have the following code that loops through a list of 150+ store numbers on "Lookup Data" tab, changes b4 cell on the "Scorecard" tab to the store number to populate the current data for that store, then exports the "Scorecard" tab plus the chart tab (which auto-populates to that store number) to a PDF with the store number as the file name. I keep getting a Compile Error : Invalid outside procedure. The export function works properly and the looping function works properly but when I put them together, I get that error. Thank you for any help you can offer. I've been struggling with this for hours.

VBA Code:
Dim i As Long, LastRow As Long
Dim SaveLocation As String
Dim rng As Range

SaveLocation = "C:\Users\cindy\OneDrive\Desktop\Consulting"

Set rng = Worksheets("Scorecard").Range("A1:h43")

LastRow = Worksheets("Lookup Tables").Range("A500").End(xlUp).Row


For i = 1 To LastRow

If Not Worksheets("Lookup Tables").Range("A" & i).Value = 0 Then
    Worksheets("Scorecard").Range("b4").Value = Worksheets("Lookup Tables").Range("A" & i).Value
    Sheets(Array("Scorecard", "Chart")).Select
    Sheets("Scorecard").Activate
    
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        SaveLocation & Range("b4").Value, Quality:= _
        xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, _
        openafterpublish:=True
End If
Next i




End Sub
 
Welcome to the Board!

It seems to be suggesting that you have some lines of code outside of procedures. All your code must be between the "Sub" and "End Sub" lines.
If you cannot find it yourself, please post ALL your code (which I don't think you did because I do not see any "Sub" line at the beginning).
 
Upvote 0
I think I fat-fingered that. This is the whole code. Thanks for looking at it!
VBA Code:
Sub PrintCerts()

Dim i As Long, LastRow As Long
Dim SaveLocation As String
Dim rng As Range

SaveLocation = "C:\Users\Cindy.Montgomery\OneDrive - USDA\Desktop"

Set rng = Worksheets("Scorecard").Range("A1:M39")

LastRow = Worksheets("Data").Range("A500").End(xlUp).Row


For i = 1 To LastRow

If Not Worksheets("Data").Range("A" & i).Value = 0 Then
    Worksheets("Scorecard").Range("C4").Value = Worksheets("Data").Range("A" & i).Value
    Sheets(Array("Scorecard", "Chart")).Select
    Sheets("Scorecard").Activate
    ActiveWindow.SmallScroll Down:=10
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        SaveLocation & Range("C4").Value, Quality:= _
        xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, _
        openafterpublish:=True
End If
Next i

       

End Sub
 
Upvote 0
Welcome to the Board!

It seems to be suggesting that you have some lines of code outside of procedures. All your code must be between the "Sub" and "End Sub" lines.
If you cannot find it yourself, please post ALL your code (which I don't think you did because I do not see any "Sub" line at the beginning).
Whoops! This is not my my day for typing. Let's try that again!

VBA Code:
Sub PrintScorecard

Dim i As Long, LastRow As Long
Dim SaveLocation As String
Dim rng As Range

SaveLocation = "C:\Users\cindy\OneDrive\Desktop\Consulting"

Set rng = Worksheets("Scorecard").Range("A1:h43")

LastRow = Worksheets("Lookup Tables").Range("A500").End(xlUp).Row


For i = 1 To LastRow

If Not Worksheets("Lookup Tables").Range("A" & i).Value = 0 Then
    Worksheets("Scorecard").Range("b4").Value = Worksheets("Lookup Tables").Range("A" & i).Value
    Sheets(Array("Scorecard", "Chart")).Select
    Sheets("Scorecard").Activate
    
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        SaveLocation & Range("b4").Value, Quality:= _
        xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, _
        openafterpublish:=True
End If
Next i

End Sub
 
Upvote 0
You are missing the ellipses after your sub name, i.e.
VBA Code:
Sub PrintScorecard
should be:
VBA Code:
Sub PrintScorecard()
 
Upvote 0
Solution
I believe the brackets are not the problem, the VBE will add it when you try to compile and not make a fuss about it. And in the first code paste there were brackets.
However , if the problem persists then this not ALL your code.
If this is all the code you need and use, check all other code modules (other modules, sheets, workbook, forms) for some misplaced line of code.
 
Upvote 0
I believe the brackets are not the problem, the VBE will add it when you try to compile and not make a fuss about it. And in the first code paste there were brackets.
However , if the problem persists then this not ALL your code.
If this is all the code you need and use, check all other code modules (other modules, sheets, workbook, forms) for some misplaced line of code.
Yeah, I see that if you try to add it without typing them in, it adds the for you automatically (which is curious that the code they posted does not show them, if they TRULY copy/pasted it here).
I think you are probably right, it is probably code somewhere else in their workbook.
 
Upvote 0

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