[英]Why is my VBA code giving me a “variable not defined” error?
几天前我发表了一个类似的帖子并得到了一些很好的建议/帮助,但我的代码似乎仍然无法正常运行。 作为参考,我正在运行 64 位 Excel 并且我的所有代码都包含在同一个模块中。
我正在尝试创建一个 function 它将读取单元格的值并播放与该值关联的特定 WAV 文件。 例如,如果 function 读取“1”,则播放“Amaj.wav”,如果播放“2”,则播放“Amin.wav”,以此类推。 我所有的 WAV 文件都存储在与我的工作簿相同的目录中。 这是我当前的代码(取自我之前帖子的答案):
Option Explicit
Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private ChordDict As New Collection
Sub ChordDictionary()
Set ChordDict = Nothing
ChordDict.Add "Amaj.wav", "1"
ChordDict.Add "Amin.wav", "2"
ChordDict.Add "Aaug.wav", "3"
ChordDict.Add "Adim.wav", "4"
ChordDict.Add "A#maj.wav", "5"
ChordDict.Add "A#min.wav", "6"
ChordDict.Add "A#aug.wav", "7"
ChordDict.Add "A#dim.wav", "8"
ChordDict.Add "Bmaj.wav", "9"
Sound 3 'you call here the function, for the third collection element
End Sub
Function Sound(Cell As Long)
Dim WAVFile As String, SoundFile As String
Const SND_ASYNC = &H1
Const SND_FILENAME = &H200000
For i = 1 To ChordDict.Count
If i = Cell Then
SoundFile = ChordDict(i)
出于某种原因,每当我尝试在 Excel 工作簿中运行“Sound()”function 时,都会收到“编译错误:变量未定义”消息。 突出显示的行是“Function Sound(Cell As Long)”。 有人对我如何按预期将我的代码获取到 function 有任何建议吗? 谢谢。
固定 function
Function Sound(Cell As Long)
Dim WAVFile As String, SoundFile As String, i as long
Const SND_ASYNC = &H1
Const SND_FILENAME = &H200000
For i = 1 To ChordDict.count
If i = Cell Then
SoundFile = ChordDict(i)
WAVFile = ThisWorkbook.path & "\" & SoundFile
Call PlaySound(WAVFile, 0&, SND_ASYNC Or SND_FILENAME)
Exit Function
End If
Next
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.