0

I have a variable with the following values:

Variable1 = "Apple, Banana, Pineaple, Grape, Coconut"

I would like to use the Replace to get the following result:

VariableX = "<span class="text1">Apple</span> <span class="text2">Banana</span> <span class="text1">Pineapple</span> <span class="text2">Grape</span> <span class="text1">Coconut</span>"

So, the first value gets text1, the second gets text2, the third gets text1, the forth gets text2 and so on, until the end.

1
  • What do you mean by "gets text1, the second gets text2..."? Commented Aug 18, 2014 at 14:32

1 Answer 1

1
' Split into an array...
a = Split(Variable1, ",")

' Add each element to a <span> tag...
For i = 0 To UBound(a)
    VariableX = VariableX & "<span class=""text" & i + 1 & """>" & Trim(a(i)) & "</span>"
Next

Update with respect to comments:

To alternate between two values you can put them in an array and use Mod to alternatively select one of the values.

a = Split(Variable1, ",")
strClasses = Array("text-info", "text-warning")

' Add each element to a <span> tag...
For i = 0 To UBound(a)
    VariableX = VariableX & "<span class=""" & strClasses(i Mod 2) & """>" & Trim(a(i)) & "</span>"
Next
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. The result is generating text1, text2, text3, text4... I am looking for just 2 strings. In my example was text1 and text2, but the real case is text-info and text-warning. Thanks again.
How are you associating text-info and text-warning with your array elements? Are you just alternating text-info and text-warning?
Yeah, just alternating. The first is text-info, the second text-warning, and loop until the end. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.