嗨,我想知道我是否可以得到一些帮助。我正在导入一个html文件,该文件将文本放入电子表格的第一列。文本将以各种方式显示,例如,它可以是颜色或斜体,粗体或组合。
有没有办法根据列的格式从列中提取各种文本,并通过VBA将文本复制到另一列?请参阅下面的示例,了解我希望实现的目标。
非常感谢提前。
屏幕截图显示了我想要实现的目标
答案 0 :(得分:0)
你可以从这段代码开始
Option Explicit
Sub Main()
Dim cell As Range
Dim word As Variant, colOff As Variant
Dim colOffStrng As String
With Worksheets("MySheet") '<~~ change "MySheet" with whatever name your sheet has
For Each cell In .Range("A4:A" & .Cells(.Rows.count, 1).End(xlUp).Row).SpecialCells(xlCellTypeConstants, xlTextValues) '<~~consider cells in column A from row 4 to the last non empty cell containing text values only
For Each word In Split(WorksheetFunction.Trim(cell)) '<~~ check each word in the cell. a space is assumed as words separator, multiple spaces considered as one space
colOffStrng = "" '<~~ initialize the offsets string
With cell.Characters(InStr(cell, word), Len(word)).Font '<~~ consider the Font property of the word first character
If .Bold Then colOffStrng = colOffStrng & "1 " '<~~ Bold is 1 column offset from column A
If .Italic Then colOffStrng = colOffStrng & "2 " '<~~ Italic is 2 columns offset from column A
Select Case .ColorIndex '<~~ consider the ColorIndex property of the word first character
Case 3 'red
colOffStrng = colOffStrng & "3 " '<~~ red is 3 columns offset from column A
Case 5 'blue
colOffStrng = colOffStrng & "4 " '<~~ blue is 4 columns offset from column A
End Select
End With
For Each colOff In Split(Trim(colOffStrng)) '<~~ loop through each column offset stored
cell.Offset(, colOff) = word '<~~ fill proper cell with the word
Next colOff
Next word
Next cell
End With
End Sub
我假设了以下内容:
每个单词的所有字符共享相同的格式
格式样式列是:
B栏中的粗体字
C列表示斜体字
红色字的列D(ColorIndex = 3)
蓝色字的列E(ColorIndex = 5)
对于他们的组合,您可以使用直接键入单元格的公式,例如
单元格H4中的 =IF(C4=D4,C4,"")
,在哪里放置&#34; Italic&#34;和&#34; Red&#34;格式化
当然&#34;组合&#34;可以使用布尔代数
进一步自动化