Home » excel » excel – How to fix "comparing userform textbox with cellvalue" code

excel – How to fix "comparing userform textbox with cellvalue" code

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am adding a record in next row with a sr.no and other data. When I enter a number in userform textbox, it must check if it is not duplicating. The numbers are in serial order

Private Sub TextBox1_Change()

    Dim x As Long
    Dim y As Worksheet

    Set y = Sheets("Data")
    x = y.Range("A" & Rows.Count).End(xlUp).Row

    If CInt(TextBox1.Value) <= CInt((Sheets("Data").Range
    .Cells(x, "A").Value)) 
    Then
        MsgBox "Duplicate serial no. found. Increase it"
    End If

End Sub

enter image description here

How to&Answers:

The code you posted might pretty much working already, but I would go for a different approach.

A serial number is a value that the system should compute, not the user should insert. My suggestion would hence be to get the next serial number with a function:

Private Function getNextSerial() As Long
    Dim x As Long
    Dim y As Worksheet

    Set y = Sheets("Data")
    x = y.Range("A" & Rows.Count).End(xlUp).Row

    getNextSerial = y.Cells(x, "A").Value + 1 
End Function

… and to set it directly in your TextBox1 when you initialize the form. For example, assuming your form is called MyUserForm, in the Initialize() event of this form you should put something like this:

Me.TextBox1.Text = getNextSerial() '<-- write next serial
Me.TextBox1.Locked = True '<-- forbid writing

If you really wanna go for the approach where the user types in the serial number, then you can still use the function getNextSerial() and write:

If Me.TextBox1.Text <= getNextSerial() Then

… however, please note that you’re doing this in a TextBox.Change event. This means that if the user wants to type 1001 as a value, with your current code they would have:

  • Typing “1” –> MsgBox pops-up
  • Continuing with “10” –> MsgBox popping up again
  • Continuing with “100” –> MsgBox popping up again!
  • End typing “1001” –> Finally!

… which would cause a high sense of hate from the users towards the system 🙂

Answer:

I agree with @MatteoNNZ. No need to ask the user for the S.No. As it is how would the user know which one is the currect user number?

I recommend 2 Options

[A] Auto insert S.No: From your image, row number will always be (Lastrow – 5) So if you are going to fill Row 9 then your S.No will be 9-5=4. For example

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long

    Set ws = Sheet1

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        '~~> Insert Sno
        .Range("A" & lRow).Value = lRow - 5
    End With
End Sub
[B] Autopopulate S.No in the form: Find the last row and subtract 5 from it and then show it in the userform. Keep the textbox locked

Private Sub UserForm_Initialize()
    Dim ws As Worksheet
    Dim lRow As Long

    Set ws = Sheet1

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

     '~~> Insert Sno
     TextBox1.Text = lRow - 5
End Sub