运行 显示用户窗体时出现错误 91 对象变量或块变量未设置

Run time Error 91 object variable or with block variable not set when showing userform

我在关闭一个用户表单并转到下一个时遇到问题。单击命令按钮后应关闭 UserForm3,并应显示 UserForm4。不幸的是我得到 "Run time Error 91 object variable or with block variable not set"。我深入研究了互联网,我很确定问题出在 Userform4 上,尽管 UserForm3 的代码被突出显示为有问题。基本上我希望显示 UserForm4,并根据 UserForm3 的 Combobox 的选择,让所有文本框填充来自 sheet "Log" 的数据。来自 UserForm3 的选择保存到 "Log" Sheet.

的单元格 E1

来自 UserForm3 的代码

Private Sub CommandButton1_Click()

Sheets("Log").Range("E1") = ComboBox2.Text
Unload Me
UserForm4.Show  <- ERROR DISPLAYED HERE

End Sub

UserForm4 中,我想在下面的单元格中找到 E1 的值,然后用找到 E1 值的行中的数据填充 Userform4 中的文本框。

UserForm4 的代码

Private Sub UserForm_Initialize()

Dim Name As String
Dim rng As Range
Dim LastRow As Long
Dim wart As Worksheet

wart = Sheets("Log").Range("E1")
LastRow = ws.Range("B3" & Rows.Count).End(xlUp).Row + 1

Name = Sheets("Log").Range("E1")
UserForm4.TextBox8.Text = Name

nazw = Application.WorksheetFunction.VLookup(wart, Sheets("Log").Range("B3:H" & LastRow), 1, False)

UserForm4.TextBox1.Text = ActiveCell.Offset(, 1)
UserForm4.TextBox2.Text = ActiveCell.Offset(, 1)
UserForm4.TextBox3.Text = ActiveCell.Offset(, 1)
UserForm4.TextBox4.Text = ActiveCell.Offset(, 1)
UserForm4.TextBox5.Text = ActiveCell.Offset(, 1)
UserForm4.ComboBox1.Text = ActiveCell.Offset(, 1)
UserForm4.TextBox6.Text = ActiveCell.Offset(, 1)
UserForm4.TextBox7.Text = ActiveCell.Offset(, 1)

End Sub

下面的代码是为了避免上面代码中提到的运行次错误,它没有针对VLookup函数部分进行调试。

Option Explicit

Private Sub UserForm_Initialize()

Dim Name        As String
Dim LastRow     As Long
Dim wart        As Variant
Dim ws          As Worksheet
Dim nazw        As Long

' set ws to "Log" sheets
Set ws = Sheets("Log")

With ws
    wart = .Range("E1")

    ' method 1: find last row in Column "B" , finds last row even if there empty rows in the middle
    LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row + 1

    ' method 2 to find last row, equivalent to Ctrl + Shift + Down
    ' LastRow = .Range("B3").CurrentRegion.Rows.Count + 1

    ' a little redundant with the line 2 above ?
    Name = .Range("E1")
End With

With Me
    .TextBox8.Text = Name

    ' ****** Need to use Match instead of Vlookup VLookup Section ******
    If Not IsError(Application.Match(wart, ws.Range("B1:B" & LastRow - 1), 0)) Then
        nazw = Application.Match(wart, ws.Range("B1:B" & LastRow - 1), 0)
    Else ' wart record not found in range
        MsgBox "Value in Sheet " & ws.Name & " in Range E1 not found in Column B !", vbInformation
        Exit Sub
    End If

    .TextBox1.Text = ws.Range("B" & nazw).Offset(, 1)
    .TextBox2.Text = ws.Range("B" & nazw).Offset(, 1)
    .TextBox3.Text = ws.Range("B" & nazw).Offset(, 1)
    .TextBox4.Text = ws.Range("B" & nazw).Offset(, 1)
    .TextBox5.Text = ws.Range("B" & nazw).Offset(, 1)
    .ComboBox1.Text = ws.Range("B" & nazw).Offset(, 1)
    .TextBox6.Text = ws.Range("B" & nazw).Offset(, 1)
    .TextBox7.Text = ws.Range("B" & nazw).Offset(, 1)
End With

End Sub