A列是原来数据,需要根据字母来做其中数字的减法字母不变,例如,A5的8S4M2L减去B2的4S2M1L能通过公式得到C2的结果,即8-4,4-2,2-1
=带字母相减(A1,B1)
输入公式下拉
'自定义公式-按ALT+F11-插入模块-粘贴代码-将表格另存为启用宏的格式
Function 带字母相减(a As String, b As String)
For i = 1 To Len(a)
c = Mid(a, i, 1)
If c Like "[A-Z]" Then
d = d & "|" & i
End If
Next
e = Mid(d, 2)
f = Split(e, "|")
For j = 0 To UBound(f)
If j = 0 Then
h = 提数(Mid(a, 1, 2))
If InStr(b, Mid(a, f(j), 1)) = 0 Then
g = 0
Else
g = 提数(Mid(b, 1, 2))
End If
Else
h = 提数(Mid(a, f(j) - 2, 2))
If InStr(b, Mid(a, f(j), 1)) = 0 Then
g = 0
Else
g = 提数(Mid(b, InStr(b, Mid(a, f(j), 1)) - 1, 1))
End If
End If
k = h - g
If k = 0 Then
Else
带字母相减 = 带字母相减 & (h - g) & Mid(a, f(j), 1)
End If
Next
If 带字母相减 = 0 Then 带字母相减 = ""
End Function
Function 提数(a As String)
For i = 1 To Len(a)
b = Mid(a, i, 1)
If b Like "[0-9]" Then 提数 = 提数 & b
Next
End Function