Solved

Excel Macro 2 x Dim declarations?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
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 …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

910 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

24 Experts available now in Live!

Get 1:1 Help Now