在VBA宏中实现两张图片同步启动、不同速度旋转的技术求助
Got it, let's fix this! The problem with using separate timers in VBA is that it's single-threaded—you can't run two timers truly concurrently. Instead, we can use a single loop that updates both shapes' rotations each iteration, but with different speed increments to get the independent rotation you want.
Here's a revised macro that does exactly what you need:
Sub RotateTwoShapesIndependently() Dim rotLittleClock As Integer Dim rotSecondShape As Integer Dim speedLittle As Integer, speedSecond As Integer ' Set your desired rotation speeds (higher values = faster rotation) speedLittle = 2 ' Little Clock rotates twice as fast speedSecond = 1 ' Second shape rotates at base speed ' Initialize rotation counters for each shape rotLittleClock = 0 rotSecondShape = 0 Do DoEvents ' Keeps Excel responsive while the loop runs ' Update each rotation counter with its unique speed rotLittleClock = rotLittleClock + speedLittle rotSecondShape = rotSecondShape + speedSecond ' Apply the new rotation to each shape Sheet1.Shapes("Little Clock").Rotation = rotLittleClock ' Replace "Second Shape" with your actual second image's name Sheet1.Shapes("Second Shape").Rotation = rotSecondShape ' Optional: Stop the loop after a set number of rotations (e.g., 10 full turns = 3600 degrees) If rotLittleClock >= 3600 Then Exit Do Loop End Sub
How this works:
- We use separate rotation counters for each shape instead of a single
rep_count, so their rotations don't tie to the same value. - The
speedLittleandspeedSecondvariables control how much each shape rotates per loop iteration. Adjust these numbers to tweak the speed ratio (e.g., 3 and 1 would make one shape 3x faster than the other). DoEventsis critical here—it lets Excel handle other tasks (like clicking buttons or scrolling) while the loop runs, so your workbook doesn't freeze up.- The optional exit condition stops the loop after 10 full rotations for the little clock; you can remove this line if you want the rotation to run until you manually stop it.
Quick tips:
- To stop the macro manually (if you skip the exit condition), press Ctrl+Break (you'll get a prompt—just click "End").
- Make sure to replace
"Second Shape"with the actual name of your second image in Sheet1. - This approach guarantees both shapes start rotating at the exact same time and maintain their speed difference throughout the loop.
内容的提问来源于stack exchange,提问作者baskinsr
相关产品推荐
相关产品推荐

