【问题标题】:Run time Error 91 object variable or with block variable not set when showing userform运行时错误 91 对象变量或显示用户窗体时未设置块变量
【发布时间】:2017-04-21 00:47:39
【问题描述】:

我在关闭一个用户窗体并转到下一个时遇到问题。单击命令按钮后的 UserForm3 应关闭并应显示 UserForm4。不幸的是,我得到“运行时错误 91 对象变量或未设置块变量”。我已经深入研究了互联网,我很确定问题出在 Userform4 上,尽管 UserForm3 的代码被突出显示为错误。基本上,我希望显示 UserForm4,并根据来自 UserForm3 的 Combobox 的选择,让所有文本框都填充工作表“Log”中的数据。来自 UserForm3 的选择被保存到“日志”表上的单元格 E1。

来自 UserForm3 的代码

Private Sub CommandButton1_Click()

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

End Sub

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

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

【问题讨论】:

  • wart = Sheets("Log").Range("E1") 是什么? wartWorksheet 对象还是 Range 对象?如果是工作表,则为 Set wart = Sheets("Log") 。如果是Range,那么Set wart = Sheets("Log").Range("E1")
  • 我希望“疣”成为“E1”单元格的内容。稍后应该使用 Vlookup 搜索它。
  • 如果“E1”包含字符串,则将Dim wart As Worksheet更改为Dim wart As String,如果E1“包含数字,则将Dim wart As Integer(或Long)更改为。您也可以使用Dim wart As Variant
  • 好的,我把它改成了Dim wart As String。现在我得到“编译错误:需要对象”,Private Sub UserForm_Initialize() 以黄色突出显示,Set wart = 以蓝色突出显示。
  • ws 在哪里设置和声明?

标签: excel runtime-error userform vba


【解决方案1】:

下面的代码是为了避免上面代码中提到的运行时错误,它没有针对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

【讨论】:

  • 现在一切都好,除了你提到的 Vlookup 部分。回答您关于ActiveCell 的问题。我认为一旦使用了 Vlookup(例如,在单元格 B4 中找到的值,即是我的活动单元格。
  • 你知道我如何“解释”到 excel 我希望我的 ActiveCell 成为发现“疣”的单元格吗?
  • @Igor 是的,使用 Match 而不是 VLookup 。在“日志”表的哪一列,所以我们需要搜索wart
  • 疣应该在B列中找到
  • @Igor 然后请用 V 标记我的答案,并添加另一篇包含您的需求和当前代码的帖子。
猜你喜欢
  • 2016-10-07
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多