【问题标题】:Time based VBA script for exercises用于练习的基于时间的 VBA 脚本
【发布时间】:2010-03-09 09:58:45
【问题描述】:

我有一个包含两列的 Excel 电子表格,其中一列包含练习列表(其中 33 个)以及每个列的重复次数。我真正想要的是一个程序,它可以选择一个随机练习,显示它的重复次数,并有一个按钮,上面写着“完成?”当你点击它时,我想要一个倒计时 20 分钟的计时器,选择一个新的练习并等到你点击完成,然后重复。

我知道这并不难,但无论如何我都不是程序员。如果有人有教程或其他方法(闪存?),我将不胜感激。

提前致谢, 周杰伦

【问题讨论】:

    标签: excel time loops vba


    【解决方案1】:

    如果您不习惯使用用户表单等,您的要求并不太难,但不太容易解释。

    相反,我提出了一个更简化的解决方案,可能适合您的需求。出于此解决方案的目的,我假设您的所有练习都列在 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
    

    最后,在您的电子表格中,您需要一种启动代码的方式。

    选择View > Toolbers > Forms,然后从菜单中单击Button 并将其绘制在电子表格的任意位置。在 Assign Macro 对话框中,您应该看到“StartExercise”作为一个选项。选择此选项并单击确定。

    现在,当您单击按钮时,您应该会看到一个练习,并且重复次数会以粗体红色字体突出显示,并且旁边会显示“20 分钟”。这将倒计时到 0 分钟。如果您然后单击该按钮,您可以通过随机练习重新开始。

    希望这会有所帮助。

    【讨论】:

    • 这太棒了!它让我想多运动! :)
    • 好消息 - 很高兴它成功了,您可以在健身房忙碌起来!还有一件事,如果解决方案适合您,那么在本网站上点击“勾选”标记接受答案将被视为礼貌。这样,其他帮助者可以看到解决方案已被接受,不需要进一步的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 2023-04-06
    • 1970-01-01
    • 2022-06-10
    • 2022-10-15
    • 1970-01-01
    相关资源
    最近更新 更多