Solved

Excel Macro 2 x Dim declarations?

Posted on 2014-09-09
1
284 Views
Last Modified: 2014-09-09
Hello,

I'm trying to define two names via the dim function. These names are being defined for two different non-active sheets.

When I run the macro below I get the message:
Duplicate declaration in current scope

- with the error highlighting row 11.

What's the best way of defining these names for non active sheets?

Sub mac_quickformulas()
'
' mac_quickformulas Macro
'
Dim uniqueIDformulas As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Unique ID")
uniqueIDformulas = ws.Range("B" & Rows.Count).End(xlUp).Row

Dim dataformulas As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Data")
dataformulas = ws.Range("B" & Rows.Count).End(xlUp).Row

'Add forumula columns to the Unique ID Sheet
    Sheets("Unique ID").Select
    Range("L2").Select
    ActiveCell.FormulaR1C1 = _
        "=COUNTIFS(Data!C[-11],"">0"",Data!C[-6],'Unique ID'!RC[-5],Data!C[-9],"">0"")"
    Range("M2").Select
    ActiveCell.FormulaR1C1 = _
        "=COUNTIFS(Data!C[-12],"">0"",Data!C[-7],'Unique ID'!RC[-6],Data!C[-10],"""")"
    Range("N2").Select
    ActiveCell.FormulaR1C1 = _
        "=(INDEX('Australia Post Codes'!C[-10],MATCH('Unique ID'!RC[-5],'Australia Post Codes'!C[-12],0)))"
    Range("N1").Select
    ActiveCell.FormulaR1C1 = "Region"
    Range("L1").Select
    ActiveCell.FormulaR1C1 = "Jobs"
    Range("M1").Select
    ActiveCell.FormulaR1C1 = "Quotes"
    Range("M2").Select
    Range("L2:N2").AutoFill Destination:=Range("L2:N" & uniqueIDformulas)

'Add formula columns to the Data Sheet
    Sheets("Data").Select
    Range("K1").Select
    ActiveWindow.SmallScroll Down:=-6
    ActiveCell.FormulaR1C1 = "Region"
    Range("K2").Select
    ActiveCell.FormulaR1C1 = _
        "=(INDEX('Australia Post Codes'!C[-7],MATCH(RC[-3],'Australia Post Codes'!C[-9],0)))"
    Range("L2").Select
    ActiveCell.FormulaR1C1 = _
        "=(INDEX('Unique ID'!C[-11],MATCH(Data!RC[-6],'Unique ID'!C[-5],0)))"
    Range("L1").Select
    ActiveCell.FormulaR1C1 = "ID"
    Range("L2").Select
    Range("K2:L2").AutoFill Destination:=Range("K2:L" & dataformulas)
    
    Sheets("Closed Address Formatting").Select
    Range("E6").Select
    
End Sub

Open in new window

0
Comment
Question by:dabug80
1 Comment
 
LVL 1

Accepted Solution

by:
dabug80 earned 0 total points
ID: 40313538
Nevermind. I figured it out. I had to change the name of the 'ws' reference to 'ws2'. See successful code:

Sub mac_quickformulas()
'
' mac_quickformulas Macro
'
Dim uniqueIDformulas As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Unique ID")
uniqueIDformulas = ws.Range("B" & Rows.Count).End(xlUp).Row

Dim dataformulas As Long
Dim ws2 As Worksheet
Set ws2 = ThisWorkbook.Sheets("Data")
dataformulas = ws2.Range("B" & Rows.Count).End(xlUp).Row

'Add forumula columns to the Unique ID Sheet
    Sheets("Unique ID").Select
    Range("L2").Select
    ActiveCell.FormulaR1C1 = _
        "=COUNTIFS(Data!C[-11],"">0"",Data!C[-6],'Unique ID'!RC[-5],Data!C[-9],"">0"")"
    Range("M2").Select
    ActiveCell.FormulaR1C1 = _
        "=COUNTIFS(Data!C[-12],"">0"",Data!C[-7],'Unique ID'!RC[-6],Data!C[-10],"""")"
    Range("N2").Select
    ActiveCell.FormulaR1C1 = _
        "=(INDEX('Australia Post Codes'!C[-10],MATCH('Unique ID'!RC[-5],'Australia Post Codes'!C[-12],0)))"
    Range("N1").Select
    ActiveCell.FormulaR1C1 = "Region"
    Range("L1").Select
    ActiveCell.FormulaR1C1 = "Jobs"
    Range("M1").Select
    ActiveCell.FormulaR1C1 = "Quotes"
    Range("M2").Select
    Range("L2:N2").AutoFill Destination:=Range("L2:N" & uniqueIDformulas)

'Add formula columns to the Data Sheet
    Sheets("Data").Select
    Range("K1").Select
    ActiveWindow.SmallScroll Down:=-6
    ActiveCell.FormulaR1C1 = "Region"
    Range("K2").Select
    ActiveCell.FormulaR1C1 = _
        "=(INDEX('Australia Post Codes'!C[-7],MATCH(RC[-3],'Australia Post Codes'!C[-9],0)))"
    Range("L2").Select
    ActiveCell.FormulaR1C1 = _
        "=(INDEX('Unique ID'!C[-11],MATCH(Data!RC[-6],'Unique ID'!C[-5],0)))"
    Range("L1").Select
    ActiveCell.FormulaR1C1 = "ID"
    Range("L2").Select
    Range("K2:L2").AutoFill Destination:=Range("K2:L" & dataformulas)
    
    Sheets("Closed Address Formatting").Select
    Range("E6").Select
    
End Sub

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now