简体   繁体   中英

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

Like the title say, I want to verify an entry and compare it with a specific cell (iLastRow, 8). For that, I put this lign before my others conditions.

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

But the message is showing every time, even if the number is lower. The strange thing is that the code works well if all the cell between column 4 to 7 are filled and doesn't work anymore if I erase the content of more than 2 side rows. There's my whole code:

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

Thanks in advance

Finally I found the problem and I changed a part of the code for that:

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

It works pretty well and it's a lot because of your help Siddharth Rout!!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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