跳到主要内容

如果在Excel中满足条件,如何播放声音?

在Excel中,我们可以应用条件格式来格式化和突出显示需要满足条件的单元格,但是有时,如果满足条件,则可能需要播放声音。 例如,如果A1中的单元格值大于300,则我想播放声音。 Excel不支持此功能,本文中,我将介绍一些VBA代码来解决此任务。

使用VBA代码根据单元格值播放默认的系统提示音

使用VBA代码根据单元格值播放自定义声音

如果单元格值通过VBA代码在特定列中发生更改,请播放声音


箭头蓝色右气泡 使用VBA代码根据单元格值播放默认的系统提示音

这是一个方便的代码,供您在满足特定条件时播放默认的系统提示音,请按照以下步骤操作:

1. 按住 ALT + F11 键,然后打开 Microsoft Visual Basic应用程序 窗口。

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。

VBA代码:根据单元格值播放默认的系统提示音:

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

3。 然后保存并关闭此代码窗口,返回到工作表,然后输入以下公式: = IF(A1> 300,BeepMe(),“”) 单元格旁边的空白单元格中包含您要基于其播放声音的值,然后按 输入 键,公式单元格中将不显示任何内容,请参见屏幕截图:

如果条件为真,则doc播放声音1

4。 现在,如果在单元格A1中输入的值大于300,将播放默认的系统提示音。


箭头蓝色右气泡 使用VBA代码根据单元格值播放自定义声音

如果您想播放默认系统提示音以外的其他声音,这里的VBA代码也可以帮您一个忙。

1。 按住 ALT + F11 键,然后打开 Microsoft Visual Basic应用程序 窗口。

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(),“”)单元格旁边的空白单元格中包含您要基于其播放声音的值,然后按 输入 键,公式单元格中将不显示任何内容,请参见屏幕截图:

如果条件为真,则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 Tab 为 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