我正在为7页的工作簿编写VBA代码。每页具有相同布局的库存数据。每只股票都有一个代码,并具有一年中每一天的开市,收市,最高,最低和交易量的数据。
我想要一个代码来为股票行情创建一个新列,并在该列旁边创建“年度变化”,“变化百分比”和“总交易量”。
我希望能够运行一次宏,并使其遍历工作簿的每个页面。它将标记该行情自动收录器,并在每页上计算总库存量,但仅计算最后一页的年度更改和百分比更改。
This is what should happen on each page
This is what all but the last page look like
从图像中可以看到,它正在完成每一页上的部分代码,但是只有最后一页才能将全部代码应用于此。
谁能告诉我发生了什么事或给我提示吗?由于I和L列已完成,因此宏肯定在每个页面上运行,但是J和K列仅在最后一页上完成。
这是我正在使用的代码: 子库存()
<int:channel id="consumeTradeJSONChannel" />
<int-jms:message-driven-channel-adapter
id="consumeTradeJSONChannelAdapter" destination="consumetradejsonmessagesqueue"
connection-factory="consumerauthenticationconnectionfactory" channel="consumeTradeJSONChannel"
acknowledge="transacted" auto-startup="true"
concurrent-consumers="100" max-concurrent-consumers="100" />
<int:channel id="buildCitiFIXChannel">
<int:interceptors>
<int:wire-tap channel="citiFIXMessageLogger" />
</int:interceptors>
</int:channel>
<int:json-to-object-transformer id="jsontoTradeBOtransformer"
input-channel="consumeTradeJSONChannel" output-channel="buildCitiFIXChannel" type="com.citigroup.valueobject.TradeDetailsVO" />
<int:transformer id="citiFIXMessageBuilder" ref="citiFIXMessageBuilder" method="buildCitiFIXmessage"
input-channel="buildCitiFIXChannel" output-channel="endOfFlowChannel" />
<bean id="citiFIXMessageBuilder" class="com.citigroup.customtransformer.FIXMessageBuilder" />
<int:channel id="endOfFlowChannel" />
<int:bridge id="endofflow" input-channel="endOfFlowChannel" output-channel="nullChannel" />
<int:logging-channel-adapter id="citiFIXMessageLogger" logger-name="citiFIXMessageLogger" level="INFO" log-full-message="true" />`
答案 0 :(得分:1)
我将避免使用对象名称来使变量变暗,因此请考虑将Sheets
中的Dim sheets as Worksheet
更改为ws
。
要遍历工作表并应用相同的逻辑,您需要执行以下操作:
Dim ws as Worksheet
For Each ws in Worksheets
'Your code goes here with all objects referring to current ws like so:
ws.Range(....
ws.Cells(....
Next ws
请勿激活工作表。而是使用变量ws
对每个对象(范围,单元格等)进行限定。我将使用查找和替换,并将Range
与ws.Range
交换,然后将Cells
与ws.Cells
交换。在您的代码中看起来像这样。
Sub Stocks()
Dim stock_ticker As String, stock_volume As Double, j As Integer
Dim ws As Worksheet
For Each ws In 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"
ws.Range("P1").Value = "Ticker"
ws.Range("Q1").Value = "Value"
ws.Range("O2").Value = "Greatest % Increase"
ws.Range("O3").Value = "Greatest % Decrease"
ws.Range("O4").Value = "Greatest Total Volume"
ws.Range("O5").Value = "Least Total Volume"
stock_volume = 0
Dim j As Integer
j = 2
For i = 2 To 43398
If ws.Cells(i, 1).Value = ws.Cells(i + 1, 1).Value Then
stock_volume = stock_volume + ws.Cells(i, 7).Value
ElseIf ws.Cells(i, 1).Value <> Cells(i + 1, 1).Value Then
stock_volume = stock_volume + ws.Cells(i, 7).Value
ws.Cells(j, 9).Value = ws.Cells(i, 1).Value
ws.Cells(j, 12).Value = stock_volume
j = j + 1
stock_volume = 0
End If
Next i
答案 1 :(得分:0)
我发现了问题。大多数股票在12/30结束,而我只检查12/31。我将第二个循环重写为:
For i = 2 To 43398
If Right(workbook_sheet.Cells(i, 2), 4) = "0101" Then
stock_year_start = workbook_sheet.Cells(i, 3)
ElseIf Right(workbook_sheet.Cells(i, 2), 4) = "1231" Then
stock_year_end = workbook_sheet.Cells(i, 6)
stock_year_change = stock_year_end - stock_year_start
stock_percent_change = stock_year_change / stock_year_start
workbook_sheet.Cells(k, 10).Value = stock_year_change
ElseIf Right(workbook_sheet.Cells(i, 2), 4) = "1230" Then
stock_year_end = workbook_sheet.Cells(i, 6)
stock_year_change = stock_year_end - stock_year_start
stock_percent_change = stock_year_change / stock_year_start
workbook_sheet.Cells(k, 10).Value = stock_year_change
If workbook_sheet.Cells(k, 10).Value > 0 Then
workbook_sheet.Cells(k, 10).Interior.ColorIndex = 4
ElseIf workbook_sheet.Cells(k, 10).Value < 0 Then
workbook_sheet.Cells(k, 10).Interior.ColorIndex = 3
End If
workbook_sheet.Cells(k, 11).Value = stock_percent_change
k = k + 1
End If
Next i
我正在考虑一种寻找年终股票价值的更优雅的方法。 43398也是一个临时值,因为每个页面上都有不同数量的要检查的股票,而且我仍在寻找一段时间来查找每个字段中的行数。
如果有人要发表评论,我会留在这里。