运行时错误' 91

时间:2018-04-13 16:29:00

标签: excel vba excel-vba

我一直收到错误:

  

运行时错误' 91;对象变量或未设置块变量。

我的脚本运行正常并完成了它需要做的事情,但我无法弄清楚如何摆脱这个错误。

感谢您的帮助。

Public Sub CommandButton1_Click()

    Dim rng As Range

    Set rng = Range("F24:I24")

    rng.Select

    If TextBox1.Text = "" Then
        MsgBox ("Must insert Temperature you dingus!")
    Else
        rng = TextBox1.Text
        Call GetCabinet1
    End If

    Unload Me

End Sub

Public Sub UserForm_Initialize()

    Dim wb As Workbook
    Dim ws As Worksheet

    Set wb = ActiveWorkbook
    Set ws = Sheets("Executive Summary")

    wb.Activate
    ws.Select

    UserForm1.Show

    Unload Me

End Sub

2 个答案:

答案 0 :(得分:2)

从你的潜艇中移除所有Unload.Me并将其放置在用户形式

的子调用中

在CommandButton1_Click()的末尾放置一个Me.Hide,而不是

最后从UserForm1.Show移除UserForm_Initialize,因为它会重复两次

所以你的“主”子看起来像是:

Sub main()

    Dim UF As UserForm1

    Set UF = New UserForm1
    UF.Show
    Unload UF ' unload the userform from here

End Sub

和你的userform1代码如:

Private Sub CommandButton1_Click()

    Dim rng As Range

    Set rng = Range("F24:I24")

    rng.Select

    If TextBox1.Text = "" Then
        MsgBox ("Must insert Temperature you dingus!")
    Else
        rng = TextBox1.Text
        Call GetCabinet1
    End If

    Me.Hide

End Sub


Public Sub UserForm_Initialize()

    Dim wb As Workbook
    Dim ws As Worksheet

    Set wb = ActiveWorkbook
    Set ws = Sheets("Executive Summary")

    wb.Activate
    ws.Select

End Sub

答案 1 :(得分:0)

只需更换卸载我的me.hide解决了我的问题...感谢所有给予他们意见的人....

相关问题