繁体   English   中英

如果 TextBox 值高于 VBA 中的特定单元格,则显示错误消息

[英]Show an error message if a TextBox value is higher than a specific cell in VBA

正如标题所说,我想验证一个条目并将其与特定单元格 (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

提前致谢

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

      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 !!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM