我一直在尝试为Excel VBA创建一个通知系统,但是我遇到了一块似乎无法解决的问题。我一直在为我创建的代码获取错误404 - 需要对象。希望你们都能提供帮助。
Public price_col As Range
Public vol_col As Range
Public Sub setVars()
Set price_col = Range("E2:E90")
Set vol_col = Range("J2:J90")
End Sub
Private Sub Worksheet_Calculate()
checkPrice price_col
checkVol vol_col
End Sub
Private Sub Worksheet_Change(ByVal target As Range)
setVars
If Not Intersect(target, price_col) Is Nothing Then
checkPrice target
End If
If Not Intersect(target, vol_col) Is Nothing Then
checkVol target
End If
End Sub
Public Sub checkPrice(target As Range)
**For Each cell In target**
Dim row As Long
row = Range(cell.Address).row
If cell.Value > 0.025 Then
If ThisWorkbook.getPriceState(row) <> 1 Then
MsgBox "Price " & Application.WorksheetFunction.RoundDown(cell.Value * 100 / 1, 0) * 1 & "% rise: " & Range(cell.Address).Offset(0, -2).Value
ThisWorkbook.setPriceState row, 1
End If
ElseIf cell.Value < -0.025 Then
If ThisWorkbook.getPriceState(row) <> -1 Then
MsgBox "Price " & Application.WorksheetFunction.RoundDown(cell.Value * 100 / 1, 0) * 1 & "% fall: " & Range(cell.Address).Offset(0, -7).Value
ThisWorkbook.setPriceState row, -1
End If
ElseIf cell.Value <> "" Then
If ThisWorkbook.getPriceState(row) <> 0 Then
ThisWorkbook.setPriceState row, 0
End If
End If
Next cell
End Sub
Public Sub checkVol(vol_col As Range)
For Each cell In vol_col
Dim row As Long
row = Range(cell.Address).row
If cell.Value >= 2.5 Then
If ThisWorkbook.getVolState(row) <> 3 Then
MsgBox "Volume Change above 250%" & Range(cell.Address).Offset(0, -7).Value
ThisWorkbook.setVolState row, 3
End If
ElseIf cell.Value >= 2 Then
If ThisWorkbook.getVolState(row) <> 2 Then
MsgBox "Volume Change above 200%" & Range(cell.Address).Offset(0, -7).Value
ThisWorkbook.setVolState row, 2
End If
ElseIf cell.Value >= 1.5 Then
If ThisWorkbook.getVolState(row) <> 1 Then
MsgBox "Volume Change above 150%" & Range(cell.Address).Offset(0, -7).Value
ThisWorkbook.setVolState row, 1
End If
ElseIf cell.Value <> "" Then
If ThisWorkbook.getVolState(row) <> 0 Then
ThisWorkbook.setVolState row, 0
End If
End If
Next cell
End Sub
我的代码“为目标中的每个单元格”出错了;这是粗体。谢谢你的帮助!
答案 0 :(得分:0)
如果您的计算事件触发,并且未设置变量,那么您将收到错误。至少,您应该在setVars
Worksheet_Calculate
的调用