Here's some example code. Assumes UID is in column A. To paste into sheet is simple as you'll just do something like Range("A" & LstRow +1) = NVal
Just FYI you might be better of changing those Variables to Public so the values can be used by your other buttons in the UserForm. So Dim changes to Public & the Variables will be outside the button code i.e Public.... then Userform_Initialize.
Dim LstRow As Long 'Figure out Last Value in Range
Dim OVal As String 'Old Value
Dim NVal As String 'New Value
With Sheet1
If .Range("A2") = "" Then
.Range("A2") = "AAFL0001"
Else
LstRow = .Cells(Rows.Count, "A").End(xlUp).Row
OVal = .Range("A" & LstRow)
NVal = "AAFL" & Format(Right(OVal, 4) + 1, "0000")
Me.TextBox1.Value = NVal
End If
End With
Obtained from the OzGrid Help Forum.
Solution provided by chirayuw.
See also: Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions and Index to new resources and reference sheets
See also:
| How to create automatic removal of empty rows |
| How to use a macro to auto delete file when passed 15 days |
| How to select an option in the combobox and the textboxes to auto-fill with data |
| How to copy master sheet as values and automatically set new name |
Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions. escorts