Dim ws As Worksheet issue

palaeontology

Active Member
Joined
May 12, 2017
Messages
444
Office Version
  1. 2016
Platform
  1. Windows
I'm receiving an error message ... (Compile error: Duplicate declaration in current scope) ... when I run some code that has 13 components, but I believe it's the following two components that are the issue ...

one of the components copies a template sheet multiple times and renames them with names from a list ...

Code:
 Dim ws As Worksheet, Ct As Long, c As RangeSet ws = Worksheets("Student Profile Template")
Application.ScreenUpdating = False
For Each c In Sheets("PrintTemplate").Range("AH49:AH100")
    If c.Value <> "" Then
        ws.Copy after:=Sheets(Sheets.Count)
        ActiveSheet.Name = c.Value
        Ct = Ct + 1
    End If
Next c
Application.ScreenUpdating = True]

another component saves all sheets that have a 5-digit name as PDFs in a certain location in G:drive ...

Code:
Dim ws As Worksheet, wPath As String, r1 As String, r2 As String, r3 As String    
    wPath = "G:\Maths Dept\STUDENT RESULTS\2019\PDF Profile Copies\"
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name Like "#####" Then
            r1 = ws.Range("S5") & "\"
            r2 = r1 & ws.Range("E2") & "\"
            r3 = r2 & ws.Range("S3") & "\"
            If Dir(wPath & r1, vbDirectory) = "" Then MkDir wPath & r1
            If Dir(wPath & r2, vbDirectory) = "" Then MkDir wPath & r2
            If Dir(wPath & r3, vbDirectory) = "" Then MkDir wPath & r3
          
            ws.PageSetup.Orientation = xlLandscape
            ws.Range("A1:ai29").ExportAsFixedFormat xlTypePDF, wPath & r3 & ws.Name & ".pdf"
        End If
    Next ws

it is the 'Dim ws As Worksheet' in the second of those two components that becomes highlighted with the error message, so I clearly can't have that appear twice, but how do I get around this ?

Any help would be appreciated.

Very kind regards,

Chris
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Are these in the same module or separate? Can't tell as you don't show the Sub Name or End Sub
 
Last edited:
Upvote 0
Hi Alan,

I've just figured out my issue ... was a very simple fix.

Ta for looking into it.

Kind regards,

Chris
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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