简体   繁体   English

如何在用户在单元格中输入值时暂停宏然后取消暂停宏

[英]How to pause macro while user enters a value in a cell and then unpause macro

I want to write an educational macro that works like this:我想编写一个像这样工作的教育宏:

The macro writes a question in A1 like "Please write the value of 5 plus 6 in cell B1".该宏在 A1 中写了一个问题,例如“请在单元格 B1 中写下 5 加 6 的值”。

The user then writes 11 in cell B1, and clicks a button.然后用户在单元格 B1 中写入 11,然后单击一个按钮。

The macro responds with "Correct" in cell C1 and writes the next question in A2.宏在单元格 C1 中以“正确”响应,并在 A2 中写入下一个问题。

In this way, the spreadsheet shows all the past questions and answers.通过这种方式,电子表格显示了所有过去的问题和答案。

Is it possible to do this - to have the macro pause while the user fills in some text and then a button unpause it so it continues from where it left off?是否有可能做到这一点 - 在用户填写一些文本时让宏暂停,然后一个按钮取消暂停它,以便它从停止的地方继续?

Many thanks!非常感谢!

If I understand you correctly and if you don't mind using a UserForm, then maybe you can try to have something like this :如果我对您的理解正确并且您不介意使用用户窗体,那么也许您可以尝试使用以下内容:

在此处输入图像描述

Prepare the question and the answer in Sheet2 something like the image above.在 Sheet2 中准备问题和答案,类似于上图。
You may want to have all the font as white, protect the sheet and hide it.您可能希望将所有字体设置为白色,保护工作表并将其隐藏。

Then the Sheet1 as where the test is, which something like below animation:然后将 Sheet1 作为测试所在的位置,类似于下面的动画: 在此处输入图像描述

Create a UserForm创建用户窗体
Make one button on it and one TextBox on it.在其上制作一个按钮,在其上制作一个 TextBox。

The sub:子:

Dim oFill As Range: Dim cnt As Long:
Dim rgQdata As Range: Dim rgQ As Range

Private Sub UserForm_Initialize()
CommandButton1.Caption = "NEXT"
Set rgQdata = Sheets("Sheet2").Range("A2")
Set rgQ = ActiveSheet.Range("A2")
rgQ.Value = rgQdata.Value
End Sub

Private Sub TextBox1_Change()
rgQ.Offset(0, 1).Value = TextBox1.Value
End Sub

Private Sub CommandButton1_Click()

    If rgQ.Offset(0, 1) = rgQdata.Offset(0, 1) Then
        rgQ.Offset(0, 2).Value = "correct"
    Else
        rgQ.Offset(0, 2).Value = "incorrect"
    End If

TextBox1.SetFocus
Set rgQdata = rgQdata.Offset(1, 0)

    If rgQdata.Value <> "" Then
        Set rgQ = rgQ.Offset(1, 0)
        rgQ.Value = rgQdata.Value
        TextBox1.Value = ""
    Else
        Unload Me
    End If

End Sub

The Initialize will make a range for the first question in the cell of the data (Sheet2) as rgQdata variable and a range for the first question asked as rgQvariable. Initialize 将为数据单元格 (Sheet2) 中的第一个问题创建一个范围作为 rgQdata 变量,并将第一个问题的范围作为 rgQvariable。 Then it put the rgQ value from the rgQdata value.然后它从 rgQdata 值中放入 rgQ 值。

When the user type the answer in the TextBox1,当用户在 TextBox1 中键入答案时,
it will fill rgQ.offset(0,1) with what he type.它将用他输入的内容填充 rgQ.offset(0,1)。

The user need to click the NEXT button to see his result.用户需要点击 NEXT 按钮才能看到他的结果。

When the user click the NEXT button, the sub check if the rgQ.Offset(0, 1) value is the same with the rgQdata.Offset(0, 1) value.当用户单击 NEXT 按钮时,子检查 rgQ.Offset(0, 1) 值是否与 rgQdata.Offset(0, 1) 值相同。 If same then put "correct" if not same then put "incorrect" in rgQ.offset(0,2) cell.如果相同,则输入“正确”,如果不相同,则在 rgQ.offset(0,2) 单元格中输入“不正确”。

Then it put the next question by offsetting one cell down of both rgQdata and rgQ.然后它通过将 rgQdata 和 rgQ 都向下偏移一个单元格来提出下一个问题。 It also check if there is no value in rgQdata then it unload the userform.它还检查 rgQdata 中是否没有值,然后卸载用户表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM