【问题标题】:Dynamically Update The Stock Ticker Array When I Add More Stocks当我添加更多股票时动态更新股票代码数组
【发布时间】:2023-04-08 05:11:01
【问题描述】:

这是我到目前为止的代码。我创建了一个空白股票代码数组,然后创建了一个名为tickerIndex 的新变量,并使用ticker(tickerIndex) 来定位数据中的特定股票代码。我使用该特定数据点将所有交易量相加,并找到该股票的起止价格并找到回报。但是当我运行它时,它只显示标题:(。

Sub AllStockAnalysis()

    Worksheets("All Stocks Analysis").Activate

    tickerIndex = 0
    totalVolume = 0
    Dim ticker() As String
    Dim startTime As Single
    Dim endTime As Single
    Dim yearValue As String
    Dim endingPrice As Double
    Dim startingPrice As Double
    RowCount = Cells(Rows.Count, "A").End(xlUp).Row

    yearValue = InputBox("What year would you like to run the analysis on?")

    startTime = Timer
    Sheets(yearValue).Activate

    For i = 2 To RowCount
        
        If Cells(i + 1, 1).Value <> Cells(i, 1).Value Or Cells(i - 1, 1).Value <> Cells(i, 1).Value Then
            Cells(i, 1).Value = ticker(tickerIndex)
            End If
        
        If Cells(i, 1).Value = ticker(tickerIndex) Then
            totalVolume(tickerIndex) = totalVolume(tickerIndex) + Cells(i, 8).Value
            End If
        
        If Cells(i - 1, 1).Value <> ticker(tickerIndex) And Cells(i, 1).Value = ticker(tickerIndex) Then
            startingPrice = Cells(i, 3).Value
            End If
            
        If Cells(i + 1, 1).Value <> ticker(tickerIndex) And Cells(i, 1).Value = ticker(tickerIndex) Then
            endingPrice = Cells(i, 6)
        
        
            Sheets("All Stocks Analysis").Cells(i + 2, "A").Value = ticker(tickerIndex)
            Sheets("All Stocks Analysis").Cells(i + 2, "B").Value = totalVolume(tickerIndex)
            Sheets("All Stocks Analysis").Cells(i + 2, "C").Value = endingPrice / startingPrice - 1
            totalVolume = 0
            tickerIndex = tickerIndex + 1
        End If
        
    
    Next i
        
    Worksheets("All Stocks Analysis").Activate
    Range("A1").Value = "All Stocks (" + yearValue + ")"
    Cells(3, 1).Value = "Ticker"
    Cells(3, 2).Value = "Total Daily Volume"
    Cells(3, 3).Value = "Return"
    endTime = Timer
    MsgBox "This code ran in " & (endTime - startTime) & " seconds for the year " & (yearValue)
  
End Sub

【问题讨论】:

    标签: arrays excel vba indexing stock


    【解决方案1】:

    您在Sheets(yearValue).Activate 之前有RowCount = Cells(Rows.Count, "A").End(xlUp).Row,因此正在获取分析表的行数(因为第一行Worksheets("All Stocks Analysis").Activate)。

    不确定您的年份电子表格 A 列是什么样的,但试试这个

    Sub AllStockAnalysis1()
    
        Dim ticker()
        Dim tickerIndex As Long, rowcount As Long, i As Long
    
        Dim startTime As Single
        Dim endTime As Single
        Dim endingPrice As Double
        Dim startingPrice As Double
        
        Dim yearValue As String
        yearValue = InputBox("What year would you like to run the analysis on?")
        startTime = Timer
        
        With ThisWorkbook.Sheets(yearValue)
            rowcount = .Cells(.Rows.Count, "A").End(xlUp).Row
            ReDim ticker(1 To rowcount, 1 To 3)
        
            For i = 2 To rowcount
                             
                ' start
                If .Cells(i, 1).Value <> .Cells(i - 1, 1).Value Then
                    tickerIndex = tickerIndex + 1
                    ticker(tickerIndex, 1) = .Cells(i, 1).Value
                End If
                
                If .Cells(i, 1).Value = ticker(tickerIndex, 1) Then
                    ' total volume
                    ticker(tickerIndex, 2) = ticker(tickerIndex, 2) + .Cells(i, 8).Value
                     
                    ' first record
                    If .Cells(i - 1, 1).Value <> ticker(tickerIndex, 1) Then
                         startingPrice = .Cells(i, 3).Value
                    End If
                     
                    ' last record
                    If .Cells(i + 1, 1).Value <> ticker(tickerIndex, 1) Then
                         endingPrice = .Cells(i, 6)
                         ' return
                         ticker(tickerIndex, 3) = endingPrice / startingPrice - 1
                    End If
                End If
            Next i
        End With
            
        With ThisWorkbook.Sheets("All Stocks Analysis")
            .Range("A1").Value = "All Stocks (" + yearValue + ")"
            .Range("A3:C3").Value = Array("Ticker", "Total Daily Volume", "Return")
            .Range("A4").Resize(tickerIndex, 3) = ticker
        End With
        endTime = Timer
        MsgBox "This code ran in " & Format(endTime - startTime, "0.0") & _
               " seconds for the year " & (yearValue)
      
    End Sub
    

    【讨论】:

    • 谢谢。我一定会试试的!
    猜你喜欢
    • 2011-01-23
    • 1970-01-01
    • 2013-01-04
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多