1

我被困在一个循环命令上。我在尝试着:

1) 将列表中的下一个股票代码复制到命名单元格中
2) 运行几个宏,这些宏将使用命名单元格中的新值
3) 循环选择列表中的下一个股票代码并重复处理直到点击列表末尾的空白单元格。

下面是我的代码,我得到了 zippo;当我运行它时甚至没有错误消息。

Sub Loop_data1()
Dim ws As Worksheet
 Set ws = Worksheets("Sheet1")   ' ws with list of tickers
Dim Rng As Range        
Set Rng = Range("a2")       ' 1st row with ticker value

Range("Stock_ticker") = Rng  '  copy value selected from ticker list to named cell stock_ticker

Do Until IsEmpty(ActiveCell)

Macro1
Macro2

ActiveCell.Offset(1, 0).Select     ' select next ticker by stepping down 1 row from present location

Loop

End Sub
4

1 回答 1

0

您的代码还不错,但我发现其中有几个问题:

  • 您没有从 ws 设置 rng(使用 Range("a2") 使用 ActiveSheet)
  • ActiveCell 是不安全的,因为您可以更改 Macro1 或 Macro2 中选定的单元格,并且您不会在没有调试的情况下看到它。

我建议接下来的改变

Dim ws As Worksheet, rng as range
Set ws = Worksheets("Sheet1")   ' ws with list of tickers
Set rng = ws.Range("a2")       ' 1st row with ticker value

Range("Stock_ticker") = rng

' here you use ActiveCell, but no activeCell was selected
' set a range you manage yourself is safer
' I set the cell with the value of rng, but if you don't want this one, change to the needed value
Do Until IsEmpty(rng)
    ' If you wanted to use ActiveCell in Macro1 or 2, it's safer to pass rng thru parameters
    call Macro1
    call Macro2

    set rng = rng.Offset(1, 0) ' select next ticker by stepping down 1 row
Loop
于 2015-07-30T20:25:07.907 回答