Hi Guys,
Could Anybody help me to format the string
aa-aa >> aa - aa
abc -ghi >> abc - ghi
In case it's aa - aa, then we don't do nothing
Thanks for your help.
Printable View
Hi Guys,
Could Anybody help me to format the string
aa-aa >> aa - aa
abc -ghi >> abc - ghi
In case it's aa - aa, then we don't do nothing
Thanks for your help.
You can try to use Replace() function but what is your actual string looks like?
yes i know the repleace function , however sometimes the string appear like hello-world, hello -world,hello- word....
So you can do something like this but I'm afraid you'd have to loop through entire string and parse it:
Code:Private Sub Command2_Click()
'thry this
Debug.Print Replace(Replace("hello-world, hello -world,hello- word", "-", " - "), Space(2), Space(1))
'Or this:
Debug.Print Replace(Replace("hello-world, hello -world,hello- word", Space(1), ""), "-", " - ")
End Sub
Is it just 2 parts with one dash in the middle? ie:
XX- XX
YY -YY
YY - YY
And not like:
XX- XX - XX - XX etc...
This is a quick/lazy way to do it. If you're dealing with a bunch of data then there are faster ways:
Code:Option Explicit
'aa-aa >> aa - aa
'abc -ghi >> abc - ghi
Private Sub FixString(TheString As String)
Dim strB() As String
strB() = Split(TheString, "-")
TheString = Trim$(strB(0)) & " - " & Trim$(strB(1))
Erase strB
End Sub
Private Sub Form_Load()
Dim s As String
s = "aa - aa"
FixString s
MsgBox s
End Sub
Nuts. Here was my nearly identical effort :(
Code:Private Function Reformat(MyString)
Dim MyArray() As String
MyArray = Split(MyString, "-")
Reformat = Trim(MyArray(0) & " - " & Trim(MyArray(1)))
End Function
Private Sub Form_Load()
MsgBox Reformat("aaa- aaa")
End Sub