我有这个For循环vba代码,其中我有一个摘要表(股票策略表),它总结了我需要从不同表格中的所有数字。
我已经将代码组合在一起,以便通过评论复制数字,供大家参考。
无论如何,似乎不起作用的是“VOLUME”部分。另外两个(ADX和关闭)工作正常,但“音量”不是。这很奇怪,因为代码执行与其他两个代码执行相同。
Private Sub pback()
Dim i As Integer
Dim wsx As Worksheet
lastrow = Sheets("Stocks Strategy").Cells(Rows.Count, 1).End(xlUp).Row
Set wsx = Sheets("Stocks Strategy")
For i = 3 To lastrow
'Close
wsx.Cells(i, 4).Value = Sheets(wsx.Cells(i, 1).Value).Range("E1048576").End(xlUp).Value
wsx.Cells(i, 5).Value = Sheets(wsx.Cells(i, 1).Value).Range("E1048576").End(xlUp).Offset(-1, 0).Value
wsx.Cells(i, 6).Value = Sheets(wsx.Cells(i, 1).Value).Range("E1048576").End(xlUp).Offset(-2, 0).Value
'Volume - not sure why its not working
wsx.Cells(i, 13).Value = Sheets(wsx.Cells(i, 1).Value).Range("S1048576").End(xlUp).Value
wsx.Cells(i, 14).Value = Sheets(wsx.Cells(i, 1).Value).Range("S1048576").End(xlUp).Offset(-1, 0).Value
wsx.Cells(i, 15).Value = Sheets(wsx.Cells(i, 1).Value).Range("S1048576").End(xlUp).Offset(-2, 0).Value
'ADX
wsx.Cells(i, 8).Value = Sheets(wsx.Cells(i, 1).Value).Range("Q1048576").End(xlUp).Value
wsx.Cells(i, 9).Value = Sheets(wsx.Cells(i, 1).Value).Range("Q1048576").End(xlUp).Offset(-1, 0).Value
wsx.Cells(i, 10).Value = Sheets(wsx.Cells(i, 1).Value).Range("Q1048576").End(xlUp).Offset(-2, 0).Value
Next i
End Sub
我的床单截图:
股票策略
(A列代表工作表名称,它将获得数字“关闭”,“ADX”和“音量”))
示例来源表格 (股票策略将获取数据的表格)
只需注意:所有工作表都在同一工作簿中。
有人会指出什么是错的吗?如果有人也可以提出更好的编码方法,我将不胜感激?
答案 0 :(得分:1)
代码中没有任何错误。无论如何,下面的内容对你来说也不适用吗?
Option Explicit
Sub pback()
Dim i As long
Dim LastRow as long
Dim DestinationSheet As Worksheet
Set DestinationSheet = workSheets("Stocks Strategy")
lastrow = DestinationSheet.Cells(Rows.Count, 1).End(xlUp).Row
Dim SourceSheet as Worksheet
Dim LastCellInColumn as range
For i = 3 To lastrow
Set SourceSheet = worksheets(wsx.Cells(i, 1).Value)
With sourcesheet
'Close '
Set LastCellInColumn = .cells(.rows.count,"E").end(xlup)
If LastCellInColumn.row > 2 then
DestinationSheet.Cells(i, 4).Value = LastCellInColumn.Value
DestinationSheet.Cells(i, 5).Value = lastcellincolumn.offset(-1,0).Value
DestinationSheet.Cells(i, 6).Value = lastcellincolumn.Offset(-2, 0).Value
Else
Msgbox("Last cell on worksheet " & .name & " detected as being on row " & LastCellInColumn.row)
End if
'Volume - not sure why its not working'
Set LastCellInColumn = .cells(.rows.count,"S").end(xlup)
If LastCellInColumn.row > 2 then
DestinationSheet.Cells(i, 13).Value = LastCellInColumn.Value
DestinationSheet.Cells(i, 14).Value = LastCellInColumn.Offset(-1, 0).Value
DestinationSheet.Cells(i, 15).Value = LastCellInColumn.Offset(-2, 0).Value
Else
Msgbox("Last cell on worksheet " & .name & " detected as being on row " & LastCellInColumn.row)
End if
'ADX'
Set LastCellInColumn = .cells(.rows.count,"Q").end(xlup)
If LastCellInColumn.row > 2 then
DestinationSheet.Cells(i, 8).Value = LastCellInColumn.Value
DestinationSheet.Cells(i, 9).Value = LastCellInColumn.Offset(-1, 0).Value
DestinationSheet.Cells(i, 10).Value = LastCellInColumn.Offset(-2, 0).Value
Else
Msgbox("Last cell on worksheet " & .name & " detected as being on row " & LastCellInColumn.row)
End if
End with
Set Lastcellincolumn = nothing
Set Sourcesheet = nothing
Next i
End Sub