|
楼主 |
发表于 2021-5-20 10:27
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
新建 Microsoft Office Excel 2007 工作表.rar
(10.67 KB, 下载次数: 92)
这是问题的原件,文件是从系统导出来的 。问题原因已经找到。单元格显示和编辑栏内容之所以不一致,是因为 第一:它的字体大小设置成了1和2 ,第二:字符串中某几个字符颜色设置成了很浅淡的颜色(比如 “a1新2型冠状疫苗” 当中的“a1”和“2”就是淡色,其他字符就是黑色)
通过查找资料,已经找到解决方案:
Sub 提取文本中黑色字符()
Dim r As Range, r2 As Range, rng As Range
Dim i&, j&, x&, y&, m&, n&, k&, b$, t$, txt$
Dim arr(), brr
Restar:
Set r = Selection.Find("*", , , , , xlPrevious)
If r Is Nothing Then MsgBox "不能选择空白区", 64, "提示": Exit Sub
Set r = Nothing
Set r = Application.Intersect(ActiveSheet.UsedRange, Selection)
If r.Cells.Count = 1 Then
For j = 1 To Len(r)
t = r.Characters(j, 1).Text
If r.Characters(j, 1).Font.Color = RGB(0, 0, 0) Then
b = b & t
End If
If b <> "" Then
txt = b
End If
Next j
b = ""
Else
x = r.Rows.Count
y = r.Columns.Count
For Each rng In r
i = i + 1
ReDim Preserve arr(1 To i)
For j = 1 To Len(rng)
t = rng.Characters(j, 1).Text
If rng.Characters(j, 1).Font.Color = RGB(0, 0, 0) Then
b = b & t
End If
If b <> "" Then
arr(i) = b
End If
Next j
b = ""
Next
ReDim brr(1 To x, 1 To y)
For m = 1 To x
For n = 1 To y
k = k + 1
brr(m, n) = arr(k)
Next
Next
End If
On Error Resume Next
Set r2 = Application.InputBox(prompt:="请选择存放的起始单元格(只需要选择一个单元格)。" & Chr(10) & "请确保其下方没有数据,以避免其他数据被覆盖", Title:="提取相同颜色文本", Type:=8)
If Err Then Exit Sub
On Error GoTo 0
If r.Cells.Count = 1 Then
With r2(1)
.NumberFormatLocal = "@"
.Value = txt
.Font.Size = 10
.Font.Name = "宋体"
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Borders.LineStyle = xlContinuous
.RowHeight = 18
.EntireColumn.AutoFit
.Parent.Activate
End With
Else
With r2(1).Resize(x, y)
.Font.Size = 10
.Font.Name = "宋体"
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Borders.LineStyle = xlContinuous
.RowHeight = 18
.NumberFormatLocal = "@"
End With
r2(1).Resize(x, y) = brr
r2(1).Resize(x, y).Columns.AutoFit
r2.Parent.Activate
End If
End Sub |
|