Excel selecting Sub-string from String

help_for_excel

Board Regular
Joined
Feb 24, 2012
Messages
72
Hi


String 1: "bold:yes";color:green;font >12; chart =no, graphic = bad
String 2: "bold:yes";color:purple;font >100; chart =no, graphic = bad
String 3: "bold:no";font >8; chart =no, graphic = very good;color:brown

my question is I want VBA code or excel formula which outputs
color:green
color:purple
color:brown

I know Mid function can be used but I am having trouble handling it. As color can appear anywhere in the string and I want whole things "Color: green"


Please help, thanks in advance
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Perhaps try this?
=MID(A2,FIND("color", A2),IFERROR(FIND(";", A2, FIND("color", A2))-FIND("color", A2), 255))

Regards
Adam
 
Upvote 0
Try

=MID(A1,FIND("color:",A1),FIND(";",A1&";",FIND("color:",A1))-FIND("color:",A1))
 
Upvote 0
Guys its not doing as it is suppose to as

color : green

but I cannot paste html code because it is onverting to graphich module
 
Last edited:
Upvote 0
Color is not always ended by :

there can be

color: green;
color:purple'
color:red<

I want function to take all this into consideration
 
Upvote 0
Do you have or can you create a list of all the possible colors that may appear in the string?

If so, try

="color:" &LOOKUP(2^15,SEARCH($E$1:$E$6,A1),$E$1:$E$6)

A1 is the string
$E$1:$E$6 is a list of possible colors
 
Upvote 0
Try this piece of code:

Sub EXTRACTCOLOUR()
Dim rcnt As Long, finalval As String, i As Long

rcnt = Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To rcnt
temp = InStr(1, Range("A" & i).Value, "color")
temp1 = InStr(temp, Range("A" & i).Value, ";")
If temp1 <> 0 Then
temp1 = temp1 - temp
finalval = Mid(Range("A" & i).Value, temp, temp1)
Else
finalval = Mid(Range("A" & i).Value, temp)
End If

Range("B" & i).Value = finalval
temp = 0
temp1 = 0
Next
End Sub

Regards...
 
Upvote 0
it doesnt work

below line gives me error

temp1 = InStr(temp, Range("A" & i).Value, ";")

error: invalid procedure call or argument
 
Last edited:
Upvote 0
Hi

Perhaps a UDF:

Code:
Function GetColor(s As String)
With CreateObject("vbscript.regexp")
  .Pattern = "\bcolor\:.+?\b"
  .IgnoreCase = True
  If .test(s) Then GetColor = .Execute(s)(0)
End With
End Function

Use in a cell like:

=GetColor(A1)

Note: it currently only returns the first match in the referenced string.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top