Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么VBA上的下标超出范围错误?_Vba - Fatal编程技术网

为什么VBA上的下标超出范围错误?

为什么VBA上的下标超出范围错误?,vba,Vba,我正在重构一段代码,该代码分析12个股票代码,并返回总交易量和每个股票代码的回报。步骤3a出现超出范围的错误,我不知道原因。你知道为什么吗 Sub-allstocksananalysisrefactured() '将startTime和endTime定义为变量 暗淡的开始时间如同单身 光阴 '询问客户希望使用输入框运行哪一年。 yearValue=InputBox(“您希望在哪一年运行分析?”) '启动计时器 开始时间=计时器 '格式化所有库存分析工作表上的输出表 工作表(“所有库存分析”)。激

我正在重构一段代码,该代码分析12个股票代码,并返回总交易量和每个股票代码的回报。步骤3a出现超出范围的错误,我不知道原因。你知道为什么吗

Sub-allstocksananalysisrefactured()
'将startTime和endTime定义为变量
暗淡的开始时间如同单身
光阴
'询问客户希望使用输入框运行哪一年。
yearValue=InputBox(“您希望在哪一年运行分析?”)
'启动计时器
开始时间=计时器
'格式化所有库存分析工作表上的输出表
工作表(“所有库存分析”)。激活
范围(“A1”).Value=“所有股票(“+yearValue+”)”
'在所有库存分析工作表上创建标题行
单元格(3,1)。Value=“Ticker”
单元格(3、2)。Value=“每日总体积”
单元格(3,3)。Value=“返回”
'初始化所有标记器的数组
变暗标记(12)为字符串
股票代码(0)=“是”
股票代码(1)=“CSIQ”
股票代码(2)=“DQ”
股票代码(3)=“ENPH”
股票代码(4)=“FSLR”
股票代码(5)=“HASI”
股票代码(6)=“JKS”
股票代码(7)=“运行”
股票代码(8)=“SEDG”
股票代码(9)=“SPWR”
股票代码(10)=“TERP”
股票代码(11)=“VSLR”
'激活数据工作表
工作表(年值)。激活
'获取要循环的行数
RowCount=单元格(Rows.Count,“A”).End(xlUp).Row
'1a)创建股票代码索引
Dim tickerIndex作为单个
tickerIndex=0
'1b)创建三个输出阵列
变暗tickerVolumes(12)与长
Dim Tickers起始价格(12)为单张
Dim TickerRendingPrices(12)为单张
''2a)创建一个for循环,将tickerVolumes初始化为零。
对于i=0到11
tickerVolumes(i)=0
''2b)在电子表格中的所有行上循环。
对于j=2的行计数
'3a)增加当前股票的交易量
**tickerVolumes(tickerIndex)=tickerVolumes(tickerIndex)+单元格(j,8)。值**
'3b)检查当前行是否是具有所选tickerIndex的第一行。
”“那么
如果单元格(j-1,1)。对单元格(j,1)赋值,则
tickerStartingPrices(tickerIndex)=单元格(j,6).值
"完"
如果结束
'3c)检查当前行是否是所选代码的最后一行
'如果下一行的tickerIndex不匹配,则增加tickerIndex。
”“那么
如果单元格(j+1)。对单元格(j,1)赋值,则
TickerRendingPrices(tickerIndex)=单元格(j,6).值
'3d增加tickerIndex。
tickerIndex=tickerIndex+1
"完"
如果结束
下一个j
'循环结束'
接下来我
'4)在阵列中循环以输出股票代码、每日总交易量和收益。
对于i=0到11
工作表(“所有库存分析”)。激活
tickerIndex=i
单元格(4+i,1)。值=tickers(tickerIndex)
单元格(4+i,2)。值=tickerVolumes(tickerIndex)
单元格(4+i,3)。值=票证呈现价格(tickerIndex)/tickerStartingPrices(tickerIndex)-1
'用粗体和下划线格式化标题
工作表(“所有库存分析”)。激活
带范围(“A3:C3”)
.Font.Bold=True
.Borders(xlEdgeBottom).LineStyle=xlContinuous
以
'在所有库存分析工作表上格式化编号结果
范围(“B4:B15”).NumberFormat=“#,#0”
范围(“C4:C15”).NumberFormat=“0.0%”
范围(“B3:B15”)。自动配合
接下来我
'分别为阳性和阴性结果添加绿色和红色背景。使用For循环和条件语句。
'为第一行和最后一行定义变量我们正在添加背景色。
dataRowStart=4
dataRowEnd=15
'开始循环
对于i=dataRowStart到dataRowEnd
'启动条件
如果单元格(i,3)>0,则
单元格(i,3).Interior.Color=vbGreen
其他的
单元格(i,3).Interior.Color=vbRed
'结束条件
如果结束
'循环结束'
接下来我
'结束计时器并打印运行代码所用的时间。
结束时间=计时器
MsgBox“此代码以”和(endTime-startTime)以及“年的秒数”和(yearValue)运行
端接头

外部
i
循环计数器似乎是“当前股票行情索引”

考虑不硬编码任何边界并一致缩进循环体:

For i = LBound(tickerVolumes) To UBound(tickerVolumes)
    tickerVolumes(i) = 0
我想你可以改变这个逻辑

Dim tickerIndex As Single
tickerIndex = 0
……为此:

Dim tickerIndex As Long
…然后删除
i
的声明,并将
i
重命名为
tickerIndex
,这将外部循环更改为:

For tickerIndex = LBound(tickerVolumes) To UBound(tickerVolumes)
    tickerVolumes(tickerIndex) = 0
现在,已经有一个循环负责递增
tickerIndex
值,因此删除“步骤3d”,删除所有注释掉的代码并修复缩进:

    For tickerIndex = LBound(tickerVolumes) To UBound(tickerVolumes)
        tickerVolumes(tickerIndex) = 0
    
        
        '2b) Loop over all the rows in the spreadsheet.
        For j = 2 To RowCount
    
            '3a) Increase volume for current ticker
            tickerVolumes(tickerIndex) = tickerVolumes(tickerIndex) + Cells(j, 8).Value
        
            '3b) Check if the current row is the first row with the selected tickerIndex.
            If Cells(j - 1, 1).Value <> Cells(j, 1) Then
                tickerStartingPrices(tickerIndex) = Cells(j, 6).Value            
            End If
        
            '3c) check if the current row is the last row with the selected ticker
            'If the next row’s ticker doesn’t match, increase the tickerIndex.
            If Cells(j + 1).Value <> Cells(j, 1) Then
                tickerEndingPrices(tickerIndex) = Cells(j, 6).Value
            End If
        Next j

    Next tickerIndex
对于tickerIndex=LBound(tickerVolumes)到UBound(tickerVolumes)
tickerVolumes(tickerIndex)=0
'2b)在电子表格中的所有行上循环。
对于j=2的行计数
'3a)增加当前股票的交易量
tickerVolumes(tickerIndex)=tickerVolumes(tickerIndex)+单元格(j,8)。值
'3b)检查当前行是否是具有所选tickerIndex的第一行。
如果单元格(j-1,1)。对单元格(j,1)赋值,则
tickerStartingPrices(tickerIndex)=单元格(j,6).值
如果结束
'3c)检查当前行是否是所选代码的最后一行
'如果下一行的代码不匹配,则增加
    For tickerIndex = LBound(tickerVolumes) To UBound(tickerVolumes)
        tickerVolumes(tickerIndex) = 0
    
        
        '2b) Loop over all the rows in the spreadsheet.
        For j = 2 To RowCount
    
            '3a) Increase volume for current ticker
            tickerVolumes(tickerIndex) = tickerVolumes(tickerIndex) + Cells(j, 8).Value
        
            '3b) Check if the current row is the first row with the selected tickerIndex.
            If Cells(j - 1, 1).Value <> Cells(j, 1) Then
                tickerStartingPrices(tickerIndex) = Cells(j, 6).Value            
            End If
        
            '3c) check if the current row is the last row with the selected ticker
            'If the next row’s ticker doesn’t match, increase the tickerIndex.
            If Cells(j + 1).Value <> Cells(j, 1) Then
                tickerEndingPrices(tickerIndex) = Cells(j, 6).Value
            End If
        Next j

    Next tickerIndex