你不需要这个 API
你也可以使用Beep
。
Sub Sample()
Beep
End Sub
例子
方式 1
如果工作表中的任何地方发生更改,此代码将运行
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
Dim CheckRange As Range
Set CheckRange = Range("C:C")
For Each Cell In CheckRange
If Cell.Text = "#N/A" Then
Beep
Exit For
End If
Next
End Sub
方式 2
上述代码的替代方案
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
For Each Cell In Columns(3)
On Error Resume Next
If CVErr(Cell) = CVErr(2042) Then
Beep
Exit For
End If
On Error GoTo 0
Next
End Sub
方式3
如果您只想在 Col C 的任何位置进行手动更改时才检查 Col C
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
If Not Intersect(Target, Columns(3)) Is Nothing Then
For Each Cell In Columns(3)
On Error Resume Next
If CVErr(Cell) = CVErr(2042) Then
Beep
Exit For
End If
On Error GoTo 0
Next
End If
End Sub
方式4
如果您想检查特定单元格中是否有手动更改
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
If Not Intersect(Target, Columns(3)) Is Nothing Then
On Error Resume Next
If CVErr(Target) = CVErr(2042) Then
Beep
Exit Sub
End If
On Error GoTo 0
End If
End Sub
方式 5
方式4的变化
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
If Not Intersect(Target, Columns(3)) Is Nothing Then
If Target.Text = "#N/A" Then
Beep
Exit Sub
End If
End If
End Sub
跟进(发表评论)
活动单元格将在 b 列中,因此它应该在 d 列中检查一个右侧 – Sam Cousins 1 分钟前
我猜你的意思是 Col C 而不是 Col D。你必须使用Worksheet_SelectionChange
这个
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Columns(2)) Is Nothing Then
If Target.Offset(, 1).Text = "#N/A" Then
Beep
End If
End If
End Sub