Vba Userform无法正常运行

Vba Userform无法正常运行,vba,excel,Vba,Excel,当我在我的用户表单上执行代码时,它有时会显示结果,有时则不会 是什么导致了这个问题 比如,当我点击execute时,它会给我一个输出,如果我用相同的参数再次执行,它不会 我不知道发生了什么事 非常感谢您的帮助 提前谢谢你 这是一个包含我的excel工作表的dropbox: Private Sub CommandButton2_Click() Me.Hide End Sub Private Sub excecute_button_Click() 'Declaring objects Dim

当我在我的用户表单上执行代码时,它有时会显示结果,有时则不会

是什么导致了这个问题

比如,当我点击execute时,它会给我一个输出,如果我用相同的参数再次执行,它不会

我不知道发生了什么事

非常感谢您的帮助

提前谢谢你

这是一个包含我的excel工作表的dropbox:

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

这是我的代码:

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 注意

我删除了我的“For I=1 to N”循环,并返回到您的“Do While Icutoff=Me.cutoff\u box.Value
N=Worksheets(“信息”).Range(“S19”).Value i=1
)您如何显示用户表单?当它不显示时,在何处以及如何停止?您是否在调试器中添加了一些断点?请提供更多详细信息。@A.S.H哦,代码运行良好;只是-当我点击execute(打开用户表单时:所以我选择了所有默认参数)时,它会在消息框中显示一个输出(ID号)。然而,如果我关闭消息框并再次执行相同的操作,它将只显示“荣誉名单”,并且在我2秒钟前刚刚执行时没有ID。当我选择和取消选择一些时也是如此;当我点击do not round up等时,它将显示ID一次,不再显示,或根本不显示任何内容。一些常规编程注释,但不会解决此问题:(1)您的“do While Icutoff=Me.cutoff\u box.Value
N=Worksheets(“Info”).Range(“S19”).Value i=1
)您如何显示用户表单?我之所以有此功能的唯一原因是因为我的一个要求是我应该拥有它。因此,这就是我为什么要这么做的原因。如果您对如何在不破坏代码完整性的情况下在代码中实现do有任何建议;很明显我做到了,我也很感激。谢谢。所以在N=工作表(“信息”)上方放置“i=i+1”等。。然后取消对该操作的评论?再次感谢,谢谢你;我认为,因为我一直按执行按钮,所以循环代码然后输出结果需要一些时间。因为当我重复两次的时候,我得到了我第一次做的预期结果。而且,当我执行默认执行,然后删除一些主题时,如果我点击执行,它将显示默认输出-但一旦我再次点击执行,它将显示一个新的输出。我肯定这和我的循环有关。无论如何,谢谢你的帮助。当我编写你的代码时,它是有效的;但是因为我需要do循环,而不破坏我已经拥有的完整性。我需要满足的要求是一个do-while或do-until、select-case和if-then语句。我注意到,每次我点击execute,它都会自动更新工作表中average列中的百分比。我认为这就是为什么某些时间某些数字出现的罪魁祸首;等。有了这个理解,也许你可以给我看一些能正确命中所有凭据的东西,并防止百分比更新。我更新了代码以恢复你的“Do-While循环”。您需要保留recalc是平均列的代码,因为如果您选择不同的主题组合,它将是不正确的。您可以手动将99%键入“平均值”列,并观察它在运行代码时对其进行更正。我之所以要这样做的唯一原因是因为我的一个要求是我应该拥有它。因此,这就是我为什么要这么做的原因。如果您对如何在不破坏代码完整性的情况下在代码中实现do有任何建议;很明显我做到了,我也很感激。谢谢。所以在N=工作表(“信息”)上方放置“i=i+1”等。。然后取消对该操作的评论?再次感谢,谢谢你;我认为,因为我一直按执行按钮,所以循环代码然后输出结果需要一些时间。因为当我重复两次的时候,我得到了我第一次做的预期结果。而且,当我执行默认执行时