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

列 C のどこかに手動で変更がある場合にのみ、列 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

Way4のバリエーション

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 の 1 つ右をチェックする必要があります – Sam Cousins 1 分前

Col DではなくCol Cを意味していたと思います。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 呼び出しやコンピューターに指定されたオーディオ ファイルを用意する必要はありません。

-EDIT- 以下のコードをテストしたところ、正しく動作するようです。

-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 ループ内にある必要があると思います。PlaySound はループから最後に割り当てられた値を保持するため、CheckRange の最後のセルが "#N/A" の場合にのみノイズが発生します。

于 2013-02-19T16:50:28.120 に答える