跳到主要內容

如果在Excel中滿足條件,如何播放聲音?

在Excel中,我們可以應用條件格式來格式化和突出顯示需要滿足條件的單元格,但是有時候,如果滿足條件,您可能希望播放聲音。 例如,如果A1中的單元格值大於300,則我想播放聲音。 Excel不支持此功能,本文中,我將介紹一些VBA代碼來解決此任務。

使用VBA代碼根據單元格值播放默認的系統提示音

使用VBA代碼根據單元格值播放自定義聲音

如果單元格值通過VBA代碼在特定列中發生更改,請播放聲音


箭頭藍色右氣泡 使用VBA代碼根據單元格值播放默認的系統提示音

這是一個方便的代碼,供您在滿足特定條件時播放默認的系統蜂鳴聲,請按照以下步驟操作:

1. 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications 窗口。

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊 窗口。

VBA代碼:根據單元格值播放默認的系統提示音:

Function BeepMe() As String
    Beep
    BeepMe = ""
End Function

3。 然後保存並關閉此代碼窗口,返回到工作表,然後輸入以下公式: = IF(A1> 300,BeepMe(),“”) 單元格旁邊的空白單元格中包含您要基於其播放聲音的值,然後按 Enter 鍵,公式單元格中將不顯示任何內容,請參見屏幕截圖:

如果條件為真,則doc播放聲音1

4。 現在,如果在單元格A1中輸入的值大於300,將播放默認的系統提示音。


箭頭藍色右氣泡 使用VBA代碼根據單元格值播放自定義聲音

如果您想播放默認系統提示音以外的其他聲音,這裡的VBA代碼也可以幫您一個忙。

1。 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications 窗口。

2. 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊 窗口。

VBA代碼:根據單元格值播放特定的聲音:

#If Win64 Then
    Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
        Alias "PlaySoundA" (ByVal lpszName As String, _
        ByVal hModule As LongPtr, ByVal dwFlags As Long) As Boolean
#Else
    Private Declare Function PlaySound Lib "winmm.dll" _
        Alias "PlaySoundA" (ByVal lpszName As String, _
        ByVal hModule As Long, ByVal dwFlags As Long) As Boolean
#End If
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
Function SoundMe() As String
'Updateby Extendoffice 20161223
    Call PlaySound("c:\windows\media\Speech On.wav", _
      0, SND_ASYNC Or SND_FILENAME)
    SoundMe = ""
End Function

3。 然後保存並關閉此代碼窗口,返回到工作表,並輸入以下公式: = IF(A1> 300,SoundMe(),“”)單元格旁邊的空白單元格中包含您要基於其播放聲音的值,然後按 Enter 鍵,公式單元格中將不顯示任何內容,請參見屏幕截圖:

如果條件為真,則doc播放聲音2

4。 從現在開始,如果將大於300的值輸入到單元格A1中,則將立即播放特定的聲音。

筆記:在上面的代碼中,您可以根據需要將聲音wav文件更改為 c:\ windows \ media \ 文件路徑。 看截圖:

如果條件為真,則doc播放聲音3


箭頭藍色右氣泡 如果單元格值通過VBA代碼在特定列中發生更改,請播放聲音

如果要在特定列中更改單元格值時播放聲音,則可以應用以下VBA代碼。

1。 右鍵單擊要在列中的值更改時播放聲音的工作表標籤,然後選擇 查看代碼 從上下文菜單中,然後在打開的 適用於應用程序的Microsoft Visual Basic 窗口,將以下代碼複製並粘貼到空白模塊中:

VBA代碼:如果單元格值在列中更改,請播放聲音:

Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice 20161223
Dim xCell As Range
On Error Resume Next
If Target.Columns.Count = 1 Then
  If Intersect(Target, Columns(3)) Is Nothing Then
    Exit Sub
  Else
    For Each xCell In Columns(3)
        On Error Resume Next
        If (xCell.Value = Target.Value) And (xCell.Value <> "") Then
          Beep
          Exit For
        End If
     Next
  End If
End If
End Sub

如果條件為真,則doc播放聲音4

備註:在上面的代碼中,數字 3 在腳本中 專欄(3) 是在此列中值更改時要播放聲音的列號。

2。 然後保存並關閉此代碼窗口,現在,如果第三列中的單元格值發生更改,則將播放默認的系統蜂鳴聲。

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (28)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I have a school project. excel user form using for search a record using barcode.. Problem is that.. i want when trig a barcode specific text box value after update with a sound file each recorded ... means a student name appear in the background. for call on closing time.. pls guide...urgent
This comment was minimized by the moderator on the site
Your article is so good I like it very much, the latest audiobooks 2022 at horbuchkostenlos.de
This comment was minimized by the moderator on the site
Good site I love this website
This comment was minimized by the moderator on the site
Thanks for the article you shared, it was very helpful for me!!!Thank you very much
You can learn the sound here: <a href="https://sonneriesvip.com/">https://sonneriesvip.com/</a>;
This comment was minimized by the moderator on the site
Thanks for the article you shared, it was very helpful. Check out some more <a href="https://yofonts.com/">font free online</a> completely free.
This comment was minimized by the moderator on the site
Bonjour,Le code ne fonctionne pas,
Le code suivant est en rouge:Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As LongPtr, ByVal dwFlags As Long) As Boolean
Pour le code ci-dessous, j'ai un fichier mp3.Call PlaySound("c:\windows\media\Speech On.wav", _
0, SND_ASYNC Or SND_FILENAME)
Faut il laisser \Speech On.WavJ'ai essayé avec mp3 mais ça ne fonctionne pas.
Pouvez vous m'aider ?MerciCordialementRobert


This comment was minimized by the moderator on the site
Can i insert a mp3 sound with durations 1 hour?

Thank you very much
You can learn the chakushinon123
This comment was minimized by the moderator on the site
Nice Work! Thank you so much for the code, it was all that I needed
This comment was minimized by the moderator on the site
Not just photos or files. Sometimes you can insert a music file into the EX at https://klingeltonkostenlos.de/klingeltone-filmmusik-gratis/. This is an interesting thing, isn't it?
This comment was minimized by the moderator on the site
Grazie molte. ho utilizzato in modo proficuo tutti i tuoi esempi che sono stati chiari e illuminanti. aiuto prezioso
This comment was minimized by the moderator on the site
I am a business person, using Excel is too familiar. I usually save the names of tracks in execl so that it is simpler to find them than to save them in files. Great.The music I use for business comes from ZigTone.com.You can go there and study them, maybe it's good for you.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations