【问题标题】:Userform works despite Run-Time Error 91. What's going on?尽管出现运行时错误 91,但用户窗体仍然有效。发生了什么事?
【发布时间】:2021-04-24 08:15:50
【问题描述】:

我是用户表单的新手。我创建了以下从子例程调用的用户表单。用户窗体从工作表中选择一个范围并创建相应数量的文本框,然后是复选框,以便为原始名称分配新名称。

用户表单是使用以下内容创建的:

Public Sub UserForm_Initialize()

    'Declare variables
    Dim txtBox As MSForms.TextBox
    Dim comBox As MSForms.ComboBox
    Dim i As Integer
    Dim j As Integer
    Dim n As Integer
    Dim dist As Integer
    Dim dstArr As Variant
    Dim rng As Range
    
    'Assign variables
    Set rng = Range("Missing_MAERSK")
    n = rng.Rows.Count
    dist = 5
    dstArr = Range("LU_Destination_Ports").Value
    
    'Loop to add textboxes
    For i = 1 To n
        Set txtBox = UserForm1.Controls.Add("Forms.TextBox.1", Visible:=True)
        With txtBox
            .name = "txtBox" & i
            .Value = rng(i)
            .Height = 20
            .Width = 150
            .Left = 81
            .Top = 30 + dist
            .Font.Size = 10
        End With
        dist = dist + 20
    Next i
    
    'Loop to add list boxes
    dist = 5
    For j = 1 To n
        Set comBox = UserForm1.Controls.Add("Forms.ComboBox.1", Visible:=True)
        With comBox
            .name = "comBox" & j
            .List = dstArr
            .Height = 20
            .Width = 150
            .Left = 315
            .Top = 30 + dist
            .Font.Size = 10
        End With
        dist = dist + 20
    Next j
    
    'Show userform
    UserForm1.Show
    
End Sub

然后,当单击“替换名称”按钮时,将运行以下命令:

Public Sub CommandButton1_Click()

    'Close userform
    Unload UserForm1
    
    'This is the one
    Dim cmb As MSForms.ComboBox
'   Dim txt As MSForms.TextBox
    Dim oldVal As String
    Dim newVal As String
    Dim rng As Range
    Dim rng2 As Range
    Dim n As Integer
    Set rng = Range("MAERSK_Destin")
    Set rng2 = Range("Missing_MAERSK")
    n = rng2.Rows.Count
    
    'Loop
    For i = 1 To n
        Set txt = Me.Controls("txtBox" & i)
        Set cmb = Me.Controls("comBox" & i)
            If cmb.Value <> "" Then
                oldVal = txt.Value
                newVal = cmb.Value
                rng.Replace what:=oldVal, Replacement:=newVal
            End If
    Next i
    
End Sub

假设我将曼谷填充到曼谷 BMT,我得到以下信息:

我认为问题可能在于我调用 Command_Button1_Click 子中的值的方式。

任何建议将不胜感激。

干杯

【问题讨论】:

  • 您不想在卸载用户表单之前让代码完成吗?
  • @Tim Williams 我的想法是在关闭表单的情况下进行操作,因为无论如何您都看不到任何事情发生。由于用户表单不可见,卸载的用户表单是否会给我运行时错误?
  • 似乎您可以隐藏用户表单,然后在完成后将其卸载?应该很容易尝试。仅供参考,在用户表单中使用Me 来引用表单而不是(例如)UserForm1 更安全 - 如果您更改名称,则无需更新代码。
  • 我用UserForm1.Hide 替换了Unload UserForm1,然后一旦循环运行,我就运行Unload UserForm1,但是有相同的错误消息。注意使用Me
  • 点击“调试”时会发生什么?

标签: excel vba userform


【解决方案1】:

找出问题所在。

据此贴:Call UserForm_Initialize from Module

不应从用户表单外部初始化用户表单。

我正在从我的子中调用UserForm_Initialize(),所以为了纠正这个问题,我将它替换为UserForm1.Show

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-21
    • 2021-08-17
    • 2014-01-08
    • 2016-04-05
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多