excel - 当前范围内缺少、重复的声明

标签 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

最佳答案

正如评论,不使用 Option Explicit变量是在第一个实例中创建的。
所以在你的代码中,rTickers执行以下行时已经创建:

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

话虽这么说,下面的行会给你一个编译错误:
Dim rTickers As Range

enter image description here

因为rTickers变量已经创建。
我会将其发布为其他人引用的答案。
但如果 R​​ory 或 Ashlee 希望添加他们的答案,我会删除我的:)。

关于excel - 当前范围内缺少、重复的声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51890127/

相关文章:

vba - 忘记保存excel宏

excel - 通过使用宏防止 ThisWorkbook 中的隐藏工作表可见并从另一个工作簿运行

vba - Excel VBA 恢复我的空格键操作

vba - 下拉选择后无法打开匹配的工作簿

vba - 创建嵌套类

excel - 1004错误:未找到任何单元格,容易解决吗?

excel - 如何将Excel函数分隔符 ";"更改为 ","?

excel - 如何将 CPLEX 解决方案写入 excel

vba - 打开/关闭标志以更改名称

vba - 根据选择插入交叉引用的宏