【问题标题】:Show an error message if a TextBox value is higher than a specific cell in VBA如果 TextBox 值高于 VBA 中的特定单元格,则显示错误消息
【发布时间】:2020-02-13 16:18:25
【问题描述】:

正如标题所说,我想验证一个条目并将其与特定单元格(iLastRow,8)进行比较。 为此,我把这个 lign 放在我的其他条件之前。

        Test = (TextBox2.Text) > ws.Cells(iLastRow, 8)
      If Test = True Then
            MsgBox "Not enough quantity in stock!"
      Else

但消息每次都会显示,即使数字较低。 奇怪的是,如果第 4 到第 7 列之间的所有单元格都被填满,代码运行良好,如果我删除超过 2 个侧行的内容,则代码不再工作。 这是我的全部代码:

Private Sub CommandButton1_Enter()

  Dim emptyRow As Long

  Dim ws As Worksheet

  Set ws = ActiveSheet

  ActiveSheet.Name = "Micrux"

  Dim iLastRow As Long, iFound As Long

  Dim rng, bEmpty As Boolean, c As Integer

  Dim Test As Boolean

  bEmpty = True


   With ws

      iLastRow = .Range("A" & .Rows.Count).End(xlUp).Row

      Set rng = .Range("A1:A" & iLastRow + 1).Find(ComboBox1.Value, _

        After:=.Range("A" & iLastRow + 1), _

        LookIn:=xlValues, _

        lookat:=xlWhole, _

        searchorder:=xlByRows, _

        SearchDirection:=xlPrevious)


      Test = (TextBox2.Text) > ws.Cells(iLastRow, 8)

    If Test = True Then

      MsgBox "Not enough quantity in stock!"

    Else

   If rng Is Nothing Then

       iFound = iLastRow + 1

   Else
       iFound = rng.Row

       For c = 4 To 7
         If Len(.Cells(iFound, c)) > 0 Then bEmpty = False
       Next

       If bEmpty = False Then
          iFound = iFound + 1
         .Cells(iFound, 1).EntireRow.Insert xlShiftDown

          .Cells(iFound, 7).Value = TextBox2.Text
          .Cells(iFound, 6).Value = TextBox3.Text
          .Cells(iFound, 5).Value = ComboBox2.Value
          .Cells(iFound, 4).Value = TextBox1.Text

     Else
          .Cells(iFound, 7).Value = TextBox2.Text
          .Cells(iFound, 6).Value = TextBox3.Text
          .Cells(iFound, 5).Value = ComboBox2.Value
          .Cells(iFound, 4).Value = TextBox1.Text
      End If

  End If

       End If

End With

Unload Me

End Sub

提前致谢

【问题讨论】:

  • 试试Test =VAL(TextBox2.Text) > VAL(ws.Cells(iLastRow, 8).Value)
  • 感谢 Siddhart Rout,它适用于 iFound 但与 iLastRow 不在同一行,它只是不显示消息..
  • 单步执行代码并检查iLastRowws.Cells(iLastRow, 8).ValueTextBox2.Text 的值是什么
  • 最后它是行不通的,除非我投入了非常高的价值..
  • 你看到我的最后一条消息了吗?

标签: excel vba textbox compare cell


【解决方案1】:

最后我发现了问题,并为此更改了部分代码:

      iFound = rng.Row
        Test = Val(TextBox2.Text) > ws.Cells(iFound, 8).Value
      If Test = True Then
        MsgBox "Not enough quantity in stock!"
      Else

它工作得很好,而且很多都是因为你的帮助 Siddharth Rout!!!!

【讨论】:

    猜你喜欢
    • 2016-07-08
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多