I have a textbox (REF1) on an userform for entering product codes, which are always 6 digits, and some have leading zeros, so I got a code off the internet (my "addLeadingZeros" function) to add the right number of leading zeros.
Ex: 12 becomes 000012
I called it from an AfterUpdate Sub so that the correct reference number could appear right after exiting the textbox.
This works just fine, but when I want to populate the worksheet with that textbox value, it populates exactly what the user has entered and not the correct reference...
Here's my code:
Private Sub REF1_afterupdate() Dim arg1 As Variant Dim MyRange As Range Dim Ans As Variant Dim ALZ As Variant Set MyRange = Sheets("Productos").Range("B2:C19000") arg1 = Me.REF1.value If Len(arg1) < 6 Then ALZ = addLeadingZeros(arg1, 6) Ans = Application.VLookup(ALZ, MyRange, 2, False) Me.REF1.value = ALZ Else: Ans = Application.VLookup(arg1, MyRange, 2, False) End If Debug.Print Ans Me.DES1.value = Ans End Sub
I am quite stuck here, any help would be much appreciated! Thanks