PDA

View Full Version : Reformat string



swaggerbox
10-17-2016, 04:56 AM
How to reformat a string that contains numeric values and place them before the textual values. Example provided below:

From:
"I have the following parts: watermelon peel 18-19, sweet almond 15-20, brown sugar 3-5, Chinese yam 20-22, radix glycyrrhizae 1.6-2, water caltrop shell 1.6-2.1, begonia birchleaf 1.2-2.1, potato 200-230"

To:
"I have the following parts: 18-19 pts. wt. watermelon peel, 15-20 pts. wt. sweet almond, 3-5 pts. wt. brown sugar, 20-22 pts. wt. Chinese yam, 1.6-2 pts. wt. radix glycyrrhizae, 1.6-2.1 pts. wt. water caltrop shell, 1.2-2.1 pts. wt. begonia birchleaf, 200-230 pts. wt. potato."

mana
10-17-2016, 05:57 AM
Option Explicit

Sub test()
Dim s As String
Dim w, v
Dim i As Long
Dim n As String

s = "I have the following parts: watermelon peel 18-19, sweet almond 15-20, brown sugar 3-5, Chinese yam 20-22, radix glycyrrhizae 1.6-2, water caltrop shell 1.6-2.1, begonia birchleaf 1.2-2.1, potato 200-230"

w = Split(Split(s, ":")(1), ",")

For i = 0 To UBound(w)
v = Split(w(i))
n = v(UBound(v))
w(i) = n & " pts. wt. " & Trim(Replace(w(i), n, ""))
Next

MsgBox Split(s, ":")(0) & " : " & Join(w, ", ")


End Sub

swaggerbox
10-17-2016, 06:16 AM
didn't think it was possible. Thanks a lot Mana!