我正在使用useform编写VBA代码,其中包含3个ComboBox和3个TextBox .

每当用户从Comboboxes提供的列表中选择任何值时,代码都会起作用 . 当我尝试将Combobox用户的选择与任何文本框组合时,问题就出现了 .

我已经为文本框分配了限制,只要识别出限制就会弹出匹配的消息框,但是当在文本框中键入适当的值时 - 我得到一个:

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

虽然我没有在整个代码中声明任何对象或任何“With”过程 . 我的代码是:

Note: Num_Molecules is the combobox name, Molecules_Hand is the textbox name.

Private Sub Insert_Molecules_Click()

Dim lMolecules As Single

If Not Len(Trim(Num_Molecules.Value)) = 0 And Len(Trim(Molecules_Hand.Text)) = 0 Then
 lMolecules = Val(Num_Molecules.Value)

   ElseIf Len(Trim(Molecules_Hand.Text)) = 0 And Len(Trim(Num_Molecules.Value)) = 0 Then
      Molecules_Hand.Text = ""
        MsgBox "enter num of molecules"
         'Exit Sub

      ElseIf Molecules_Hand.Value < 5 And Len(Trim(Num_Molecules.Value)) = 0 Then
        Molecules_Hand.Text = ""
           MsgBox "value cannot be under 5"
            'Exit Sub

          ElseIf Molecules_Hand.Value >= 5 And Len(Trim(Num_Molecules.Value)) = 0 Then
           If Not Val(Molecules_Hand.Value) = Int(Val(Molecules_Hand.Value)) Then
             MsgBox "number must be an integer"
              'Exit Sub

               ElseIf IsNumeric(Molecules_Hand.Value) = False And Len(Trim(Num_Molecules.Value)) = 0 Then
                 MsgBox "textbox or choice cannot be empty"
                  'Exit Sub
            End If

Else
  lMolecules = Index_Form.Molecules_Hand.Value


End If
End Sub

问题出在“End If”之前的最后一行 - 它只是不会将文本框值赋给变量 . 我的所有3个文本框都有同样的问题,并且它们都以相同的方式分配 .

我很感激你的帮助 .