我对VBA还是很陌生,很难理解为什么我的代码会不断返回相同的运行时错误1004。我们的任务是获取一个工作表,其中工作表的数量为x(每年我们提供的数据)可以提供一年中每一天的所有股票的所有价值,然后打印出报价器的名称,年度变化,变化百分比和总交易量。
到目前为止,代码一直在第一张纸上运行,但是当它尝试移动到下一张纸上时,它在行ws.Cells(row,1).Value <> ws.Cells处给了我一个运行时错误。 (行+ 1,1)。然后,值
我查找了循环浏览表格的不同方法,但我仍然得到了同样的东西。
Sub testing()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Range("I1").Value = "Ticker"
ws.Range("J1").Value = "Yearly Change"
ws.Range("K1").Value = "Percent Change"
ws.Range("L1").Value = "Total Stock Volume"
Dim ticker As String
Dim yearly_change As Double
yearly_change = 0
Dim opening As Double
Dim closing As Double
Dim row As Double
Dim counter As Double
counter = 2
For row = 2 To Rows.Count
If ws.Cells(row, 1).Value <> ws.Cells(row - 1, 1).Value Then
opening = ws.Cells(row, 3).Value
End If
If ws.Cells(row, 1).Value <> ws.Cells(row + 1, 1).Value Then
ticker = ws.Cells(row, 1).Value
closing = ws.Cells(row, 6).Value
yearly_change = closing - opening
total_stock_volume = total_stock_volume + Cells(row, 7).Value
ws.Range("I" & counter).Value = ticker
ws.Range("J" & counter).Value = yearly_change
ws.Range("K" & counter).Value = yearly_change / opening
ws.Range("K" & counter).NumberFormat = "0.00%"
ws.Range("L" & counter).Value = total_stock_volume
counter = counter + 1
total_stock_volume = 0
Else
total_stock_volume = total_stock_volume + ws.Cells(row, 7).Value
yearly_change = closing - opening
End If
Next row
Next ws
End Sub
我希望代码能遍历所有工作表,但只能运行第一工作表