Excel 通过重复代码循环

Excel 通过重复代码循环,excel,vba,Excel,Vba,我正在使用一个电子表格,将tdameritrade的实时价格输入一个单元格,公式为:=RTD(“tos.RTD”,“LAST”,B4)。假设公式带来的起始价格是30美元。如果该配方带来的下一个价格上升到30.02美元,电池颜色将变为绿色。如果该配方带来的下一个价格降到30.01美元,电池颜色将变为红色。因此,如果从上一个价格上涨,电池将变为绿色,如果从上一个价格下跌,电池将变为红色。当我尝试在单元格B4、B5、B6、B7中添加更多结果时,我复制了代码,代码正常工作,但是继续添加代码是不实际的,因

我正在使用一个电子表格,将tdameritrade的实时价格输入一个单元格,公式为:=RTD(“tos.RTD”,“LAST”,B4)。假设公式带来的起始价格是30美元。如果该配方带来的下一个价格上升到30.02美元,电池颜色将变为绿色。如果该配方带来的下一个价格降到30.01美元,电池颜色将变为红色。因此,如果从上一个价格上涨,电池将变为绿色,如果从上一个价格下跌,电池将变为红色。当我尝试在单元格B4、B5、B6、B7中添加更多结果时,我复制了代码,代码正常工作,但是继续添加代码是不实际的,因为我需要数千个结果。任何人都知道如何改变代码,使其循环,因为我自己还不够高级,但很想学习如何

谢谢

工作表代码

 Private Sub Worksheet_Calculate()
      Call updateme
    End Sub

    Private Sub Worksheet_Change(ByVal Target As Range)
      Call updateme
    End Sub


    Private Sub updateme()
      Set cell = ActiveSheet.Range("B4")
      newval = cell.Value
      If newval < lastval Then
        cell.Interior.ColorIndex = 3
      End If
      If newval > lastval Then
        cell.Interior.ColorIndex = 4
      End If
      Set cell2 = ActiveSheet.Range("B5")
      newval2 = cell2.Value
       If newval2 < lastval2 Then
        cell2.Interior.ColorIndex = 3
      End If
      If newval2 > lastval2 Then
        cell2.Interior.ColorIndex = 4
      End If
        Set cell3 = ActiveSheet.Range("B6")
      newval3 = cell3.Value
       If newval3 < lastval3 Then
        cell3.Interior.ColorIndex = 3
      End If
      If newval3 > lastval3 Then
        cell3.Interior.ColorIndex = 4
      End If
          Set cell4 = ActiveSheet.Range("B7")
      newval4 = cell4.Value
       If newval4 < lastval4 Then
        cell4.Interior.ColorIndex = 3
      End If
      If newval4 > lastval4 Then
        cell4.Interior.ColorIndex = 4
      End If
     lastval = newval
     lastval2 = newval2
     lastval3 = newval3
     lastval4 = newval4
    End Sub

**Module Code**

Public lastval As Double
Public lastval2 As Double
Public lastval3 As Double
Public lastval4 As Double
Private子工作表_Calculate()
调用updateme
端接头
私有子工作表_更改(ByVal目标作为范围)
调用updateme
端接头
私有子updateme()
设置单元格=ActiveSheet.Range(“B4”)
newval=单元格值
如果newvallastval,则
cell.Interior.ColorIndex=4
如果结束
Set cell2=ActiveSheet.Range(“B5”)
newval2=cell2.Value
如果newval2lastval2,则
cell2.Interior.ColorIndex=4
如果结束
设置cell3=ActiveSheet.Range(“B6”)
newval3=cell3.Value
如果newval3lastval3,则
cell3.Interior.ColorIndex=4
如果结束
设置cell4=ActiveSheet.Range(“B7”)
newval4=cell4.Value
如果newval4lastval4,则
cell4.Interior.ColorIndex=4
如果结束
lastval=newval
lastval2=newval2
lastval3=新Val3
lastval4=newval4
端接头
**模块代码**
公共lastval为双精度
公共lastval2为双精度
公共服务的最后价格为双倍
公营房屋第四期为双倍

您可以使用For while和do while

下面的for循环可能会帮助您

Private Sub updateme()
暗淡单元格作为范围
'根据需要更改i的开始和结束范围
对于i=4到7
'根据需要更改此处的列
设置单元格=ActiveSheet.Range(“B”和i)
newval=单元格值
如果newvallastval,则
cell.Interior.ColorIndex=4
如果结束
lastval=newval
下一个

End Sub
使用先前值的静态数组。顺便说一句,我很确定RTD函数不会触发工作表的更改,只会触发工作表的计算。它不起作用了。它将B4和B5变为红色,B6-B8变为绿色,且无任何更新。谢谢,这只是一个你(B4,B5,B6,B7)所期望的循环的例子!请检查答案中的链接并根据您的需要继续:)嗨,Linga,我再次尝试了它,它成功了,所以非常感谢您。我只是有一个问题,每次我添加一个新的股票符号,它会给我一个类型不匹配的错误。如果你能提供更多的洞察力,那就太棒了。再次感谢。很高兴知道这是有效的。请接受我的回答,这将有助于其他人!请提出另一个问题,包括新发行的代码和样本日期:)