当我在用户表单上执行代码时,有时会显示结果,而有时却不显示结果。
是什么引起了这个问题?
就像,当我点击执行时,它会给我一个输出,如果我再次使用相同的参数来输出它,它将不会。
我不知道这是怎么回事。
任何和所有帮助,不胜感激。
在此先感谢您。
这是带有我的excel工作表的保管箱:
这是我的代码:
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
显示按钮代码:
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
请制作工作簿的备份副本,然后将“荣誉榜”中的所有代码替换为以下内容,看看是否仍然存在问题。我做了一些更改,现在无法使其失败。
注意我删除了“ For i = 1 to N”循环,并返回到“ Do While i <N”循环的修改版本。您需要使用“ Do While i <= N”,因为您的代码从未处理过最后一项。我还将“ i = i + 1”代码移到了“ Do”循环中的最后一条指令上。通过将其放在循环的中间,您总是会跳过第一个“ If average.Cells ...”代码。
尽管我将前面提到的代码移到了循环之外,但是在执行此操作之前它已经起作用了。而且,不相关,但是我重命名了您的变量,所以我知道我使用的是哪种类型-但这也不是解决方案。
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句