用户窗体不能正常工作

当我在用户表单上执行我的代码时,有时会显示结果,而其他时间则不显示。

什么可能导致这个问题?

就像,当我执行它时,会给我一个输出,如果我再次使用相同的参数,它不会。

我不知道发生了什么事。

任何和所有的帮助表示赞赏。

先进的谢谢你。

这里是我的Excel表格的保存箱:

点击这里进入保pipe箱

这是我的代码:

Private Sub CommandButton2_Click() Me.Hide End Sub Private Sub excecute_button_Click() 'Declaring objects Dim N As Long, i As Long, subjectCount As Long Dim tmp_avg As Double Dim student As Range Dim reading As Range, writing As Range, grammar As Range, spelling As Range Dim math As Range, science As Range, social As Range Dim average As Range Dim info As Worksheet Dim cutoff As String Dim cutoff_score As Double Dim output As String 'Setting objects Set info = Worksheets("Info") Set student = Range(info.Cells(6, 3), info.Cells(55, 3)) Set reading = Range(info.Cells(6, 5), info.Cells(55, 5)) Set writing = Range(info.Cells(6, 6), info.Cells(55, 6)) Set grammar = Range(info.Cells(6, 7), info.Cells(55, 7)) Set spelling = Range(info.Cells(6, 8), info.Cells(55, 8)) Set math = Range(info.Cells(6, 9), info.Cells(55, 9)) Set science = Range(info.Cells(6, 10), info.Cells(55, 10)) Set social = Range(info.Cells(6, 11), info.Cells(55, 11)) Set average = Range(info.Cells(6, 13), info.Cells(55, 13)) 'Counting subjects subjectCount = Me.readingBox.Value + _ Me.writingBox.Value + _ Me.grammarBox.Value + _ Me.spellingBox.Value + _ Me.mathBox.Value + _ Me.scienceBox.Value + _ Me.socialBox.Value 'Reading cut-off cutoff = Me.cutoff_box.Value N = Worksheets("Info").Range("S19").Value i = 1 Do While i < N 'Computing average tmp_avg = (reading.Cells(i, 1) * Me.readingBox.Value + _ writing.Cells(i, 1) * Me.writingBox.Value + _ grammar.Cells(i, 1) * Me.grammarBox.Value + _ spelling.Cells(i, 1) * Me.spellingBox.Value + _ math.Cells(i, 1) * Me.mathBox.Value + _ science.Cells(i, 1) * Me.scienceBox.Value + _ social.Cells(i, 1) * Me.socialBox.Value) / subjectCount 'Rounding If Me.Round.Value = True Then average.Cells(i, 1).Value = WorksheetFunction.Ceiling(tmp_avg, 0.01) Else average.Cells(i, 1).Value = tmp_avg End If i = i + 1 'Checking whether student met honor roll requirements Select Case cutoff Case "A+" cutoff_score = 0.96 Case "A" cutoff_score = 0.93 Case "A-" cutoff_score = 0.9 Case "B+" cutoff_score = 0.86 Case "B" cutoff_score = 0.83 Case "B-" cutoff_score = 0.8 Case "C+" cutoff_score = 0.76 Case "C" cutoff_score = 0.73 Case "C-" cutoff_score = 0.7 End Select If average.Cells(i, 1).Value >= cutoff_score Then output = output & student.Cells(i, 1).Value & " " End If Loop MsgBox "HONOR ROLL" & vbNewLine & output End Sub Private Sub UserForm_Click() End Sub 

显示button代码:

 Sub honor_roll_button() With honor_roll_form 'Loading combo box .cutoff_box.Clear .cutoff_box.AddItem "A+" .cutoff_box.AddItem "A" .cutoff_box.AddItem "A-" .cutoff_box.AddItem "B+" .cutoff_box.AddItem "B" .cutoff_box.AddItem "B-" .cutoff_box.AddItem "C+" .cutoff_box.AddItem "C" .cutoff_box.AddItem "C-" .cutoff_box.Value = "B+" 'Setting default check boxes honor_roll_form.readingBox = True honor_roll_form.writingBox = True honor_roll_form.grammarBox = True honor_roll_form.spellingBox = True honor_roll_form.mathBox = True honor_roll_form.scienceBox = True honor_roll_form.socialBox = True honor_roll_form.Round = True 'Showing Form .Show End With End Sub Sub test() Dim x As String x = "hello" x = x & vbNewLine & "goodbye" MsgBox x End Sub 

请制作工作簿的备份副本,然后将荣誉证书表格中的所有代码replace为以下内容,看看是否仍有问题。 我做了一些改变,现在不能让它失败。

注意我删除了“For i = 1到N”循环,并返回到“Do While i <N”循环的修改版本。 您需要使用“Do While I <= N”,因为您的代码从未处理过最后一个项目。 我也移动了'i = i + 1'代码作为'Do'循环中的最后一个指令。 通过你在循环中,你总是跳过第一个“If average.Cells …”的代码。

虽然我把前面提到的代码移到了循环中,但是在执行之前,它正在工作。 而且,不相关,但我重命名了你的variables,所以我会知道我使用的是什么types – 但这也不是一个解决scheme。

 Option Explicit Private Sub CommandButton2_Click() Me.Hide End Sub Private Sub excecute_button_Click() 'Declaring objects Dim N As Long, i As Long, lSubjectCount As Long Dim dTmp_Avg As Double Dim rngStudent As Range Dim rngReading As Range, rngWriting As Range, rngGrammar As Range, rngSpelling As Range Dim rngMath As Range, rngScience As Range, rngSocial As Range Dim rngAverage As Range Dim wsInfo As Worksheet Dim sCutoff As String Dim dCutoff_score As Double Dim strOutput As String 'Setting objects Set wsInfo = Worksheets("Info") Set rngStudent = Range(wsInfo.Cells(6, 3), wsInfo.Cells(55, 3)) Set rngReading = Range(wsInfo.Cells(6, 5), wsInfo.Cells(55, 5)) Set rngWriting = Range(wsInfo.Cells(6, 6), wsInfo.Cells(55, 6)) Set rngGrammar = Range(wsInfo.Cells(6, 7), wsInfo.Cells(55, 7)) Set rngSpelling = Range(wsInfo.Cells(6, 8), wsInfo.Cells(55, 8)) Set rngMath = Range(wsInfo.Cells(6, 9), wsInfo.Cells(55, 9)) Set rngScience = Range(wsInfo.Cells(6, 10), wsInfo.Cells(55, 10)) Set rngSocial = Range(wsInfo.Cells(6, 11), wsInfo.Cells(55, 11)) Set rngAverage = Range(wsInfo.Cells(6, 13), wsInfo.Cells(55, 13)) 'Counting subjects lSubjectCount = Me.readingBox.Value + _ Me.writingBox.Value + _ Me.grammarBox.Value + _ Me.spellingBox.Value + _ Me.mathBox.Value + _ Me.scienceBox.Value + _ Me.socialBox.Value 'Reading cut-off sCutoff = Me.cutoff_box.Value 'Checking whether student met honor roll requirements '### Move before your loop - no need to do this 50 times. Select Case sCutoff Case "A+" dCutoff_score = 0.96 Case "A" dCutoff_score = 0.93 Case "A-" dCutoff_score = 0.9 Case "B+" dCutoff_score = 0.86 Case "B" dCutoff_score = 0.83 Case "B-" dCutoff_score = 0.8 Case "C+" dCutoff_score = 0.76 Case "C" dCutoff_score = 0.73 Case "C-" dCutoff_score = 0.7 End Select N = Worksheets("Info").Range("S19").Value i = 1 Do While i <= N 'Computing average dTmp_Avg = (rngReading.Cells(i, 1) * Me.readingBox.Value + _ rngWriting.Cells(i, 1) * Me.writingBox.Value + _ rngGrammar.Cells(i, 1) * Me.grammarBox.Value + _ rngSpelling.Cells(i, 1) * Me.spellingBox.Value + _ rngMath.Cells(i, 1) * Me.mathBox.Value + _ rngScience.Cells(i, 1) * Me.scienceBox.Value + _ rngSocial.Cells(i, 1) * Me.socialBox.Value) / lSubjectCount 'Rounding If Me.Round.Value = True Then rngAverage.Cells(i, 1).Value = WorksheetFunction.Ceiling(dTmp_Avg, 0.01) Else rngAverage.Cells(i, 1).Value = dTmp_Avg End If If rngAverage.Cells(i, 1).Value >= dCutoff_score Then 'Debug.Print "+++" & rngStudent.Cells(i, 1).Value & vbTab & rngAverage.Cells(i, 1).Value strOutput = strOutput & rngStudent.Cells(i, 1).Value & " " Else 'Debug.Print "---" & rngStudent.Cells(i, 1).Value & vbTab & rngAverage.Cells(i, 1).Value End If i = i + 1 Loop MsgBox "HONOR ROLL" & vbNewLine & strOutput End Sub Private Sub UserForm_Click() End Sub