从单元格中具有多种颜色的单元格中提取字体颜色 [英] Extracting font color from cells with multiple colors in the cell
问题描述
我有一个Excel工作表,正试图进入MySQL数据库.
I have an Excel sheet that I am trying to get into a MySQL database.
- 我正在使用VBA将数据以文本形式写到文件中,然后将其上传到数据库中.
- 在工作表的单元格中,已使用颜色编码了字符串.
- 颜色具有一定的含义,因此在将值移入数据库时我想保留它们(我在数据库中有一个特殊的列,用于枚举颜色).
问题是,有些单元格中的字符串用逗号分隔,而逗号的一侧是黑色,另一侧是蓝色(反之亦然,单元格中可能有更多的逗号和字符串).
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).
我尝试过的
我可以使用VBA中的Split
函数提取字符串,但是会丢失字符串的格式.
I can extract the strings fine by using the Split
function in VBA but that loses the formatting of the string.
我可以使用Range("mycell").Font.ColorIndex
获取单元格的颜色,但是如果字符串中的颜色超过一种,则返回NULL
.
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)为黑色(颜色索引-4105),
2.(PO3244)将为蓝色(颜色索引47)
3.(12309)为红色(颜色索引3).
"W345, PO3244, 12309"
1. (W345) would be black (colorindex -4105),
2. (PO3244) would be blue (colorindex 47)
3. (12309) would be red (colorindex 3).
推荐答案
我将使用.Font.Color
剔除RGB值,但是您可以根据需要将其更改为ColorIndex.
I'd use .Font.Color
to cull the RGB values, but you can change it to ColorIndex if you like.
您可以调整此策略:
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
这篇关于从单元格中具有多种颜色的单元格中提取字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!