gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:30:20 27 4
gpt4 key购买 nike

我目前正在编写一个程序来分析一些数据,而我的调试器正在抛出“当前范围内的重复声明”错误,突出显示“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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com