。我有一个excel电子表格,其中包含一些字符串,其中包含在Windows 7中不可见的unicode控制字符。因此,我想编写一个宏来遍历列中的每个单元格,检查是否存在控制字符。如果找到一个控制字符,我想填充下一列中的相邻单元格,其中包含字符名称和可在字符串中找到的索引。
这是我到目前为止所做的:
Sub control_chr()
'
' control_chr Macro
'
'
Dim control_characters(Chr(28), Chr(29), Chr(30), Chr(31), Chr(32))
Dim r As Range, cell As Range
Set r = Range("F4:F1029")
For Each cell In r
For Each Character In control_characters
下一步是搜索单元格中的每个字符,并用结果填充相邻单元格。我的第一个想法是使用SEARCH()函数,因为它返回找到字符的索引。这是我第一次使用visual basic,我不知道如何继续
答案 0 :(得分:1)
以下是一些符合您要求的代码:
Sub ListControlChars()
Dim control_characters As Variant
Dim r As Range, cell As Range, ResultCell As Range
Dim CharPosition As Long
Dim i As Long
control_characters = Array(28, 29, 30, 31, 32)
Set r = ActiveSheet.Range("F4:F1029")
For Each cell In r
Set ResultCell = cell.Offset(0, 1)
ResultCell.ClearContents
CharPosition = 0
For i = LBound(control_characters) To UBound(control_characters)
CharPosition = InStr(cell, Chr(control_characters(i)))
If CharPosition > 0 Then
ResultCell = ResultCell.Value & "Char " & control_characters(i) & ": Position " & CharPosition & " - "
End If
Next i
Next cell
End Sub
如果您想在Excel中执行此操作,可以将其设置为:
B2中的公式为:
=IFERROR(SEARCH(CHAR(B$1),$A2),"")