View Full Version : Replace char in string - basic question


epenney
10-09-2009, 06:08 AM
I have a series of numbers each containing 7 characters and I need to replace the 3rd character with a 2 if it is 1 and a 1 if it is 2.
e.g. 1111111 becomes 1121111 and 2222222 becomes 2212222

I can't simply use the replace function so any ideas how I do it? Thanks

DCrake
10-09-2009, 06:17 AM
Welcome,

You need to employ a function to do that

Public Function SwapDigits(AnyNumber as Long) As Long

nStr = CStr(AnyNumber)

If Mid(nStr,3,1) = "1" Then
nStr = Left(nStr,2) & "2" & Right(nStr,4)
ElseIf Mid(nStr,3,1) = "2" Then
nStr = Left(nStr,2) & "1" & Right(nStr,4)
End If

SwapDigits = CLng(nStr)

End Function

David