我有一个包含两列的 Excel 电子表格,一列包含练习列表(其中 33 个)和每个列的代表次数。我真正想要的是一个程序,它可以选择一个随机练习,显示它的重复次数,并有一个按钮,上面写着“完成?”当你点击它时,我想要一个倒计时 20 分钟的计时器,选择一个新的练习并等到你点击完成,然后重复。
我知道这并不难,但无论如何我都不是程序员。如果有人有教程或其他方法(闪存?),我将不胜感激。
提前致谢,
周杰伦
最佳答案
如果您不习惯使用用户表单等,您所要求的并不太难,但也不容易解释。
相反,我提出了一个更简化的解决方案,可能适合您的需求。出于此解决方案的目的,我假设您的所有练习都列在 A 列中,而代表列在 B 列中。以下代码将随机选择一个练习,突出显示所选的选择,然后在 C 列中显示从 20 分钟到0 间隔 1 分钟。作为可视化:
A B C
1 Bench press 20 reps
2 Abs 10 reps
3 Lateral raise 15 reps 14 mins <-display of minutes remaining
4 Bicep curl 8 reps
5 Calf raise 10 reps
6 etc
为此,首先将以下代码添加到模块中( Alt + F11 ,然后 Insert > Module )
Sub StartExercise()
'Get number of exercises
Dim NumberOfExercises As Integer
NumberOfExercises = Range("A1").End(xlDown).Row - 1
'Reset font to normal black and clear anything in column C
Range("A1:B" & NumberOfExercises + 1).Font.Bold = False
Range("A" & NumberOfExercises + 1 & ":" & "B" & NumberOfExercises + 1).Font.ColorIndex = 1
Range("C1:C" & NumberOfExercises + 1).Clear
'Select a random exercise
Dim RandomExercise As Integer
RandomExercise = Int(Rnd() * (NumberOfExercises - 1 + 1) + 1)
'Highlight selected exercise and reps
Range("A" & RandomExercise + 1 & ":" & "B" & RandomExercise + 1).Font.Bold = True
Range("A" & RandomExercise + 1 & ":" & "B" & RandomExercise + 1).Font.ColorIndex = 3
'Countdown from 20 minutes to 0
SetCountDown RandomExercise
End Sub
Sub SetCountDown(TargetCellRow As Integer)
Dim MinsRemaining As Integer
Dim iMins As Integer
MinsRemaining = 20
For iMins = MinsRemaining To 0 Step -1
Range("C" & TargetCellRow + 1).Value = iMins & " mins"
Application.Wait (Now + TimeValue("0:01:00"))
Next iMins
End Sub
最后,在您的电子表格上,您将需要一种启动代码的方式。
选择 查看 > 工具 > 表格 然后从菜单中单击
Button
并将其绘制在电子表格的任何位置。在 分配宏 对话框中,您应该看到“StartExercise”作为选项。选择此选项并单击确定。现在,当您单击按钮时,您应该会看到一个练习,并且重复次数会以粗体红色字体突出显示,并且旁边会显示“20 分钟”。这将倒计时到 0 分钟。如果您然后单击该按钮,您可以通过随机练习重新开始。
希望这可以帮助。
关于excel - 用于练习的基于时间的 VBA 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2408076/