8

我目前有这个代码:

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 列有错误,则会播放可听见的声音,但是它不起作用,有什么想法吗?

4

5 回答 5

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
于 2013-02-19T16:41:21.260 回答
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
于 2013-02-19T16:41:38.763 回答
1

将 Cell.Value 更改为 Cell.Text

如果存在公式错误,则单元格值将类似于错误 2042,但您的 if 语句正在查找文本“#N/A”

我还建议只使用使用过的范围而不是整个列,因为这会减少运行时间。

如果发现错误,您也可以立即退出 for。

使用 Beep 不会让您播放任何声音,但它会发出可听见的声音,并且不需要 API 调用或计算机具有指定的音频文件。

-编辑- 我刚刚测试了下面的代码,它似乎对我来说可以正常工作。

-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
于 2013-02-19T16:51:29.537 回答
0

尝试替换为If PlaySound = True Then

于 2013-02-19T16:38:02.210 回答
0

我认为您检查 PlaySound 值的 If 语句应该在您的 For 循环中。您编写它的方式只会在 CheckRange = "#N/A" 中的最后一个单元格时发出噪音,因为 PlaySound 将保存从循环中分配给它的最后一个值。

于 2013-02-19T16:50:28.120 回答