Excel VBA: 拆分包含特定结构字符串的代码
Sub SplitString() '定义变量 Dim str As String Dim num1 As String Dim num2 As String Dim c As String Dim eng As String
'循环读取每个单元格的值
For Each cell In Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
'获取单元格的值
str = cell.Value
'判断字符串中是否包含'1S'和'C',以及'C'后面是否跟着数字
If InStr(str, "1S") > 0 And InStr(str, "C") > 0 And IsNumeric(Mid(str, InStr(str, "C") + 1, 1)) Then
'获取数字1和数字2
num1 = Left(str, InStr(str, "1S") - 1)
num2 = Mid(str, InStr(str, "C") + 1, 1)
'获取'C'和英文字母
c = Mid(str, InStr(str, "C"), 2)
eng = Mid(str, InStr(str, "C") + 2)
'将拆分后的数据写入相邻的单元格
cell.Offset(0, 1) = num1
cell.Offset(0, 2) = c
cell.Offset(0, 3) = num2
cell.Offset(0, 4) = eng
End If
Next cell
End Sub
原文地址: http://www.cveoy.top/t/topic/mJE7 著作权归作者所有。请勿转载和采集!