Solved

Excel Macro 2 x Dim declarations?

Posted on 2014-09-09
1
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

726 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