I have an Excel sheet that I am trying to get into a MySQL database.
The thing is that some cells have strings separated by commas and on one side of the comma the string is black, on the other side it is blue (or vice versa and there can be more commas and strings in the cell).
what I have tried
I can extract the strings fine by using the Split
function in VBA but that loses the formatting of the string.
I can get the color of a cell using Range("mycell").Font.ColorIndex
but that returns NULL
if there is more then one color in the string.
Is it possible to get all the colors of a string?
Example: one cell could contain the following string
"W345, PO3244, 12309"
1. (W345) would be black (colorindex -4105),
2. (PO3244) would be blue (colorindex 47)
3. (12309) would be red (colorindex 3).
VBA: Extract text based on font color Then save and close the dialog, and select a blank cell next to the data list, type this formula =GetColorText(A1) (A1 indicates the cell you want to extract text from), press Enter key to get the needed text, then drag autofill handle to fill the formula to the range you want.
On the Data tab, click Filter. in the column that contains the content that you want to filter. Under Filter, in the By color pop-up menu, select Cell Color, Font Color, or Cell Icon, and then click the criteria.
In Excel, there is no direct way for you to filter rows by multiple colors, but, you can create a VBA code to return the color index number of the corresponding row in a new column, and then filter the rows by this helper column.
You can use the following and then create a dictionary/collection/array to store the colors and only keep unique values or whatever solution fits your situation. This just shows how you can access all the colors.
Sub AllColors()
Dim r As Range
Dim x As Integer
Set r = Selection
For x = 1 To Len(r.Value)
Debug.Print r.Characters(x, 1).Font.ColorIndex
Next x
End Sub
I'd use .Font.Color
to cull the RGB values, but you can change it to ColorIndex if you like.
You can adapt this strategy:
Sub CellColors2CSV()
Dim j&, k&, c$, r As Range
Set r = ActiveSheet.Cells(1, 1)
Do
j = Len(r)
k = InStr(k + 1, r, ",")
If k Then j = k - 1
c = c & "," & r.Characters(j, 1).Font.Color
Loop Until k = 0
c = Mid$(c, 2)
MsgBox c
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With