Solved

Excel Macro 2 x Dim declarations?

Posted on 2014-09-09
1
290 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

16 Experts available now in Live!

Get 1:1 Help Now