簡體   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