[RESOLVED] Function to Check String Format and Replace with a Different Format
Hi guys,
I already search everywhere but I can't really find anything regarding this problem.
Let us say I have several strings in my word application in this format "11:59:00" (##:##:##). *Note is doesn't necessarily mean that there 'time'. It's just that they are written in that format.
What I what is to replace all instances of that string format into "11.59.00" (##.##.##). How do I do that?
I can see that I can work this through using the find functions wild cards of do it on vb code. But I really can't find any material that will help me on this matter. Thank you in advance.
Re: Function to Check String Format and Replace with a Different Format
You should just be able to use the vb Replace function, try this
vb Code:
Sub test()
Dim aString As String
testString = "11:59:00"
testString = Replace(testString, ":", ".")
Msgbox testString
End Sub
Re: Function to Check String Format and Replace with a Different Format
Try this
Code:
Sub repwords()
Dim r As Range, i As Integer
Dim StringOld As String, StringNew As String
Set r = ActiveDocument.Range
For Each r In ActiveDocument.StoryRanges
For i = 1 To Len(r.Text)
If Mid(r.Text, i, 1) = ":" And Mid(r.Text, i + 3, 1) = ":" Then
StringOld = Mid(r.Text, i - 2, 8)
StringNew = Replace(StringOld, ":", ".")
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = StringOld
.Replacement.Text = StringNew
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End If
Next i
Next r
End Sub
Re: Function to Check String Format and Replace with a Different Format
Thank you very much. I got it.