我开发了一个Excel实时数据馈送(RTD)来监控股票到货时的价格
我希望找到一种方法来在价格变化时改变单元格的颜色。
例如,当价值发生变化时,最初为格林的单元格将变为红色(通过它包含的RTD公式在其上发生新价格),然后在新价格到达时更改为绿色,依此类推......
答案 0 :(得分:3)
也许这可以帮助你入门? 我想在刷新实时数据时会引发一个事件。 概念sis将实时数据存储在变量中并检查它是否已更改
Dim rtd As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ActiveSheet.Range("A1")
If .Value <> rtd Then
Select Case .Interior.ColorIndex
Case 2
.Interior.ColorIndex = 3
Case 3
.Interior.ColorIndex = 4
Case 4
.Interior.ColorIndex = 3
Case Else
.Interior.ColorIndex = 2
End Select
Else
.Interior.ColorIndex = 2
End If
rtd = .Value
End With
End Sub
答案 1 :(得分:1)
Sub Worksheet_Change(ByVal ChangedCell As Range)
' This routine is called whenever the user changes a cell.
' It is not called if a cell is changed by Calculate.
Dim ColChanged As Integer
Dim RowChanged As Integer
ColChanged = ChangedCell.Column
RowChanged = ChangedCell.Row
With ActiveSheet
If .Cells(RowChanged, ColChanged).Interior.ColorIndex = 19 Then
' Changed cell is red. Set it to green.
.Cells(RowChanged, ColChanged).Interior.ColorIndex = 19
Else
' Changed cell is not red. Set it to red.
.Cells(RowChanged, ColChanged).Interior.ColorIndex = 19
End If
End With
End Sub
答案 2 :(得分:0)
此解决方案会重新发布Calculation
事件。我不完全确定RTD更新是否会触发此操作,因此您需要进行实验。
将此代码添加到包含RTD呼叫的Worksheet
模块。
它从最后一次计算中将工作表数据的副本保留在内存中,并在每个计算中比较新值 它将其作用限制在含有您的配方的细胞中。
Option Explicit
Dim vData As Variant
Dim vForm As Variant
Private Sub Worksheet_Calculate()
Dim vNewData As Variant
Dim vNewForm As Variant
Dim i As Long, j As Long
If IsArray(vData) Then
vNewData = Me.UsedRange
vNewForm = Me.UsedRange.Formula
For i = LBound(vData, 1) To UBound(vData, 1)
For j = LBound(vData, 2) To UBound(vData, 2)
' Change this to match your RTD function name
If vForm(i, j) Like "=YourRTDFunction(*" Then
If vData(i, j) <> vNewData(i, j) Then
With Me.Cells(i, j).Interior
If .ColorIndex = 3 Then
.ColorIndex = 4
Else
.ColorIndex = 3
End If
End With
End If
End If
Next j, i
End If
vData = Me.UsedRange
vForm = Me.UsedRange.Formula
End Sub
答案 3 :(得分:0)
前面的答案都假设实时数据Feed会触发工作表事件。我无法在RTD文件中找到任何信息来确认或否认这一假设。但是,如果它确实触发了工作表事件,我会认为Worksheet_Change最有用,因为它标识了一个已更改的单元格。
以下可能值得一试。它必须放在相关工作表的代码区域中。
Option Explicit
Sub Worksheet_Change(ByVal ChangedCell As Range)
' This routine is called whenever the user changes a cell.
' It is not called if a cell is changed by Calculate.
Dim ColChanged As Integer
Dim RowChanged As Integer
ColChanged = ChangedCell.Column
RowChanged = ChangedCell.Row
With ActiveSheet
If .Cells(RowChanged, ColChanged).Font.Color = RGB(255, 0, 0) then
' Changed cell is red. Set it to green.
.Cells(RowChanged, ColChanged).Font.Color = RGB(0, 255, 0)
Else
' Changed cell is not red. Set it to red.
.Cells(RowChanged, ColChanged).Font.Color = RGB(255, 0, 0)
End If
End With
End Sub
答案 4 :(得分:0)
我一直在寻找相同的东西。我的场景就像从列表中选择值时更改单元格的颜色。每个列表项对应一种颜色。
最终对我有用的是:
Private Sub Worksheet_Change(ByVal Target As Range)
Set MyPlage = Range("B2:M50")
For Each Cell In MyPlage
Select Case Cell.Value
Case Is = "Applicable-Incorporated"
Cell.Font.Color = RGB(0, 128, 0)
Case Is = "Applicable/Not Incorporated"
Cell.Font.Color = RGB(255, 204, 0)
Case Is = "Not Applicable"
Cell.Font.Color = RGB(0, 128, 0)
Case Else
Cell.EntireRow.Interior.ColorIndex = xlNone
End Select
Next
ActiveWorkbook.Save
End Sub
答案 5 :(得分:0)
或者,最简单的是这段代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Target.Interior.ColorIndex = 6 ': yellow
End Sub