当前范围中缺少重复的声明

时间:2018-08-17 07:11:21

标签: excel-vba

我当前正在编写一个程序来分析一些数据,并且我的调试器抛出“当前范围中的重复声明”错误,突出显示“ Dim rTickers As Range”。我在这里找不到任何副本。还有其他原因导致我收到此错误吗?谢谢你的时间。

Sub TSV_Ticker()
    'Create Dictionary To get Unique Values From Column A
    Dim dTickers As New Dictionary
    Dim i As Long
    For i = 2 To Rows.Count
        On Error Resume Next
        dTickers.Add (Cells(i, 1).Value), CStr(Cells(i, 1).Value)
    Next

    'Create The Ticker And Sum Column Headers
    Range("J1").Value = "<Sum>"
    Range("I1").Value = "<Ticker>"

    'Define where we will be putting our keys
    Dim uTickers As Range
    Set uTickers = Range("I2")
    'Convert Keys into array for syntax reasons
    aTickers = dTickers.Keys
    'Resize the range so it will fit the array
    Set rTickers = rTickers.Resize(UBound(aTickers), 1)
    'Put array into range, verticaly
    rTickers.Value = Application.Transpose(aTickers)

    'Define Range of column A
    Dim rTickers As Range
    Set rTickers = Range("A2:A" & ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row)
    Dim TSV As Long

    'Defining some Date Variables (Column B)
    Dim fDate As Integer
    Dim eDate As Integer
    Dim rDates As Range
    Set rDates = Range("B2:B" & ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row)

    'And The Open / Close Variables (Colums C&F)
    Dim vOpen As Double
    Dim Vclose As Double
    Dim Delta As Double
    Dim pDelta As Double

    'Adding Some Columns
    sht.Range("J1").EntireColumn.Insert
    Range("J1").Value = "Yearly Change"
    sht.Range("K1").EntireColumn.Insert
    Range("K1").Value = "Percent Change"

    For Each Cell In rTickers
        'Searching our range that we put the Array in for matching Values
        Set t = rTickers.Find(Cell.Value, LookIn:=xlValues, lookat:=xlWhole)
        If Not t Is Empty Then
            'add column G's value to corresponding I value
            Cells(t.Row, 10).Value = Cells(t.Row, 10).Value + Cells(Cell.Row, 7).Value
        End If
    Next Cell
End Sub

1 个答案:

答案 0 :(得分:1)

如前所述,不使用Foo变量是在第一个实例中创建的。
因此,在您的代码中,执行以下这一行时已经创建了Option Explicit

rTickers

话虽这么说,下面的行会给您一个编译错误:

Set rTickers = rTickers.Resize(UBound(aTickers), 1)

enter image description here

因为已经创建了Dim rTickers As Range 变量。
我会将其发布为答案,以供其他参考。
但是,如果Rory或Ashlee希望添加他们的答案,我将删除我的:)。