|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub 解析文字内容()
' 定义字母含义的映射数组
Dim letterMap As Variant
letterMap = Array("P-压力", "F-流量", "I-指示", "C-控制", "A-报警", "T-温度", "L-液位")
' 获取A列的数据范围
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
' 遍历A列中的每一行,解析文字内容并写入B列
Dim i As Long
For i = 1 To lastRow
Dim content As String
content = Cells(i, "A").Value
' 解析第一个字母
Dim firstLetter As String
firstLetter = Left(content, 1)
Dim meaning As String
meaning = ""
Dim j As Long
For j = LBound(letterMap) To UBound(letterMap)
If Left(letterMap(j), 1) = firstLetter Then
meaning = Mid(letterMap(j), 3)
Exit For
End If
Next j
' 解析后续字母
Dim restLetters As String
restLetters = Mid(content, 2)
Dim parts As Variant
parts = Split(restLetters, "-")
Dim k As Long
For k = LBound(parts) To UBound(parts)
For j = LBound(letterMap) To UBound(letterMap)
If Left(letterMap(j), 1) = parts(k) Then
meaning = meaning & "-" & Mid(letterMap(j), 3)
Exit For
End If
Next j
Next k
' 将解析结果写入B列
Cells(i, "B").Value = meaning
Next i
End Sub
|
|