ipbr21054
Well-known Member
- Joined
- Nov 16, 2010
- Messages
- 5,738
- Office Version
- 2007
- Platform
- Windows
Morning,
I am not sure which part of the code below adds a following digit of 1 each time the same name is entered.
If the name exists then a 2 would be added to the end of that name.
Should then that name be entered again then this time it is followed by 3,in the future 4,5,6 etc etc
Can we identify & edit the part of the code that applies this as i would like to have it apply 002 or 003 etc etc as opposed to the 2,3 etc that is currently happening.
I am not sure which part of the code below adds a following digit of 1 each time the same name is entered.
If the name exists then a 2 would be added to the end of that name.
Should then that name be entered again then this time it is followed by 3,in the future 4,5,6 etc etc
Can we identify & edit the part of the code that applies this as i would like to have it apply 002 or 003 etc etc as opposed to the 2,3 etc that is currently happening.
Code:
Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean) Dim fndRng As Range, findString As String, i As Integer
If Me.TextBox2.Value = "" Then Exit Sub
findString = Me.TextBox2.Value
With Sheets("POSTAGE").Range("B:B")
Set fndRng = .Find(What:=findString, LookIn:=xlValues, lookat:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If Not fndRng Is Nothing Then
'what was entered already exists - alter the name until not found
For i = 2 To 20
findString = Me.TextBox2.Value & " " & i
Set fndRng = .Find(What:=findString, LookIn:=xlValues, lookat:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If fndRng Is Nothing Then Exit For
Next i
'message saying what name should be
'MsgBox "The name to use is " & findString
'enter that name into textbox 2
With Me.TextBox2
.Value = findString
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
'cancel moving out of text box
Cancel = True
End If
End With