我目前有这段代码:
Option Explicit
Private Declare Function sndPlaySound32 Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName _
As String, ByVal uFlags As Long) As Long
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
Dim CheckRange As Range
Dim PlaySound As Boolean
Set CheckRange = Range("C:C")
For Each Cell In CheckRange
If Cell.Value = "#N/A" Then
PlaySound = True
End If
Next
If PlaySound Then
Call sndPlaySound32("C:\windows\media\chord.wav", 1)
End If
End Sub
我试图得到它,以便如果C列中有错误,会播放声音,但是它不起作用,有什么想法吗?
答案 0 :(得分:9)
您不需要此
的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
答案 1 :(得分:4)
只需粘贴以下代码并运行,看看它是否有效......
Option Explicit
Private Declare Function sndPlaySound32 Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName _
As String, ByVal uFlags As Long) As Long
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
Dim PlaySound As Boolean
If Target.Column = 3 Then
For Each Cell In Target
If WorksheetFunction.IsNA(Cell.Value) Then
PlaySound = True
End If
Next
If PlaySound Then
Call sndPlaySound32("C:\windows\media\chord.wav", 1)
End If
End If
End Sub
答案 2 :(得分:1)
将Cell.Value更改为Cell.Text
如果存在公式错误,则单元格值将是错误2042行,但您的if语句正在查找文本“#N / A”
我还建议仅使用已使用的范围而不是整列,因为这会减少运行所需的时间。
如果发现错误,您也可以立即退出。
使用Beep不会允许您播放任何声音,但它会发出声音,并且不需要API调用或计算机具有指定的音频文件。
CNC中 我刚刚测试了下面的代码,它似乎对我有用。
-EDIT2- 更正代码
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
Dim CheckRange As Range
Set CheckRange = Range(Cells(1, 3), Cells(ActiveSheet.UsedRange.Rows.Count, 3))
For Each Cell In CheckRange
If Cell.Text = "#N/A" Then
Beep
Exit For
End If
Next
End Sub
-EDIT3- 原始帖子的工作副本
Option Explicit
Private Declare Function sndPlaySound32 Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName _
As String, ByVal uFlags As Long) As Long
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
Dim CheckRange As Range
Dim PlaySound As Boolean
Set CheckRange = Range(Cells(1, 3), Cells(ActiveSheet.UsedRange.Rows.Count, 3))
For Each Cell In CheckRange
If Cell.Text = "#N/A" Then
PlaySound = True
Exit For
End If
Next
If PlaySound Then
Call sndPlaySound32("C:\windows\media\chord.wav", 1)
End If
End Sub
答案 3 :(得分:0)
尝试替换为If PlaySound = True Then
答案 4 :(得分:0)
我认为检查PlaySound值的If语句应该在For循环中。你写它的方式只会在CheckRange中的最后一个单元格为“#N / A”时产生噪音,因为PlaySound将保持从循环中分配给它的最后一个值。