code autonumber into textbox on userform based on lastrow doesn't work

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
397
Office Version
  1. 2016
Platform
  1. Windows
Hi
experts
I have this code , but doesn't work correctly . when I add item into textbox 2 ,then should search for the lastrow in column A and autonumber in textbox1 .if the number in last row for column A is 1 , then when I add item into textbox 2 . the number should be 2 into textbox1 and if the lastrow in column A =2 ,then when I add item into textbox 2 the number should be 3 into textbox1
but it autonumbers starting 1 without see into last row in column A even if last row in column A 2 or 3 ...
VBA Code:
Private Sub TextBox2_Change()
If TextBox2 <> "" Then
ks = WorksheetFunction.CountA(Range("a2:a1000"))
TextBox1.Value = ks + 1
Else
TextBox1.Value = ""
End If
End Sub
any suggestion to fix it,please?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try
VBA Code:
Private Sub TextBox2_Change()
    If TextBox2 <> "" Then
        With Range("a2:a1000")
            TextBox1.Text = Val(Cstr(.Cells(WorksheetFunction.CountA(.Cells), 1).Value)) + 1
        End With
    Else
        TextBox1.Value = ""
    End If
End Sub
 
Upvote 0
Solution
great ! this is very helpful .;)
many thanks buddy :)
 
Upvote 0

Forum statistics

Threads
1,214,972
Messages
6,122,530
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top