作为一个更大项目的一部分,我需要在一个宏中显示一系列图像。当我运行它时,它会在最后一个 msgbox 之后显示最终图像,其他图像在下面:
Sub Macro4()
Dim x As Integer
Dim Pic As Object
Dim picname As String
For x = 1 To 7
picname = ThisWorkbook.Path & "/" & "pic" & x & ".png"
ActiveSheet.Pictures.Insert(picname).Select
MsgBox (x)
Next x
End Sub
Msgbox 命令可以减慢处理速度,以便我可以看到,或者在这种情况下看不到,图片会发生变化。
这些图像被称为 pic1.png、pic2.png 等
如何在宏期间显示单独的图像?
重新编辑:
所以这里是图片功能和播放随机音乐混凝土的较大功能。
Private Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long
showpic给出与正在播放的声音的音高相关的图像。
Function showpic(value)
Dim v As Integer
v = value
picname = ThisWorkbook.Path & "/" & "pic" & v & ".png"
ActiveSheet.Shapes.AddPicture (picname), True, True, a1, a1, 170, 170
DoEvents
End Function
play运行一系列特定的声音文件,这些文件由随机过程生成,选择乐器和音高并创建必要的文件名。“piece”运行“notes”秒,并由一个单独的宏触发,该宏更改给定“单元格”中的值以匹配“条件”。
Function play(Cell, condition, notes)
Dim WAVFile As String
Dim x As Integer
Dim y As Integer
Dim z As Integer
Dim c As String
Dim d As String
Dim currentcell As String
Dim pitchcell As String
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
On Error GoTo ErrHandler
For x = 1 To notes
If Evaluate(Cell.value & condition) Then
y = x + 65
z = x + 39
c = Chr(y)
d = Chr(z)
If y > 90 Then c = "A" & d
currentcell = c & 6
pitchcell = c & 3
showpic (Sheets("Sheet1").Range(pitchcell).value)
WAVFile = ThisWorkbook.Path & "/" & Sheets("Sheet1").Range(currentcell).Text & ".wav"
Call PlaySound(WAVFile, 0&, SND_SYNC)
End If
Next x
ErrHandler:
play = False
Exit Function
End Function
我有三个问题:
- 它不显示第一个图像文件
- 它在开始时执行“计算”或 F9,因此重新随机播放乐曲并且不播放显示的序列;我认为这是由第一个 DoEvents 引起的。
- 它现在播放两次!但是,第二次通过它确实显示了所有图像文件。