VBA - Want to load Red colored text from a cell into an array
I want to put all the RED colored text in a cell , into an Array. Any help will be appreciated.
Below is situation -
My cell contain this text -> " This is testing to Red color. "
I want to populate an array Like , Array(0) = 'testing' and Array(1) = 'color'.
I am able to create a function which is scanning the entire cell which gives me 'testingcolor' . I am not able to identify the spaces. i will be good if this function can give me 'testing color'.Then i can scan for spaces and create the array. T
Code:
Function GetColorText(pRange As Range) As String
Dim xOut As String
Dim xValue As String
Dim i As Long
Dim j As Long
xValue = pRange.Text
j = VBA.Len(xValue)
For i = 1 To VBA.Len(xValue)
If pRange.Characters(i, 1).Font.Color = vbRed Then
xOut = xOut & VBA.Mid(xValue, i, 1)
End If
Next
GetColorText = xOut
End Function
Thanks Okami , p45cal and snb for your suggestions. I have other issue now....
Thanks Okami , p45cal and snb for your suggestions. I tried the suggestion from p45cal and it worked exactly the way i wanted. I appreciate all your help . However ,i ran into another problem. Your suggestion works fine when i am using a single cell. But my requirement changed and i had to use a merged cell now. This is not working for a merged cell. I have a function which gives me the merged cell data into a variable. I get the merged data and put it into xValue, but then i don't get the correct red colored text.
Can you guys let me know how can i make it work ?
I changed my code to a Subroutine instead of a function............don't ask me why ? Partial reason is because i wanted to pass array as a parameter and passing array to Subroutine is easy but passing array to function is a bit complex and was giving me various errors. i tried the options as highlighted in Blue color. I thought that i can get the data whatever i can and then assign it to xValue. But seems that xValue will only work when it picks the data from an individual cell using .TEXT property.
*** Note: I am able to do it somehow but i don't like the way i did it as it is just a patch. So what i did is , before calling the subroutine, i copied the merged cell data to a single cell and passed that cell to the subroutine.
When i place the below red code, immediately above my function, it works fine. :banghead:
Range("A6:E13").Select Selection.Copy
Range("P8").Select
ActiveSheet.Paste
Call GetColorText(Range("P8"), Coloredtext)
=============================
Sub GetColorText(pRange As Range, Coloredtext As String)
Dim xOut As String
Dim xValue As String
Dim i As Long
Dim j As Long
'Dim getcolortxt As String
'x = GetMergedCellValue(13, 5)
'Range("P8").Value = x
xValue = pRange.Text
'xValue = GetMergedCellValue(13, 5)
j = VBA.Len(xValue)
For i = 1 To VBA.Len(xValue)
If pRange.Characters(i, 1).Font.Color = vbRed Then
xOut = xOut & VBA.Mid(xValue, i, 1)
Else
xOut = xOut & " "
End If
Next
' GetColorText = xOut
Coloredtext = Application.Trim(xOut)
End Sub
=============================
Quote:
Originally Posted by
p45cal
Okami, if the whole string is red yours ends up with blank cell (also the first and last rows are a bit odd):
Attachment 24100
My quick and dirty offering with minimal alterations to the original function:
Code:
Function GetColorText(pRange As Range) As String
Dim xOut As String
Dim xValue As String
Dim i As Long
Dim j As Long
xValue = pRange.Text
j = VBA.Len(xValue)
For i = 1 To VBA.Len(xValue)
If pRange.Characters(i, 1).Font.Color = vbRed Then
xOut = xOut & VBA.Mid(xValue, i, 1)
Else
xOut = xOut & " "
End If
Next
GetColorText = Application.Trim(xOut)
End Function
1 Attachment(s)
Thanks p45cal and rothstein but still have another issue....
when i type my reply , it gives error "New posts are scanned for number of URL and forbidded words". I had typed my reply in a word documnet.
Thanks p45cal and rothtein for your help. Can u guys give a look to my word document attached here. Let me ask 1 more question.
On Button1Click ---- I prepared few arrays.
Ob Button2Click ---- i want to use the arrays i created on Button1click. VBA is not allowing me to do so ?? Please help. Refer word doc. for details.