'按楼上的思路写了点代码,不敢保证是最简单的 Private Sub Command1_Click() Dim strData(0 To 1) As String Dim strTemp() As String Dim i As Long, j As Long
strData(0) = "AVB4-1" strData(1) = "AB5-36"
For i = 0 To UBound(strData) strTemp = Split(strData(i), "-") strTemp(1) = Format(strTemp(1), "000") For j = Len(strTemp(0)) To 1 Step -1 If IsNumeric(Mid(strTemp(0), j, 1)) = False Then strTemp(0) = Left(strTemp(0), j) & Format(Val(Right(strTemp(0), Len(strTemp(0)) - j)), "00") Exit For End If Next strData(i) = strTemp(0) & "-" & strTemp(1) Debug.Print strData(i) Next
End Sub
Private Sub Command2_Click() MsgBox trans("AB5-36") & vbCrLf & trans("AvB4-1") End Sub Function trans(ByVal x As String) As String Dim temp As String temp = Left(x, InStr(x, "-")) & " " Mid(temp, Len(temp) - 2) = "0" & Trim(Right(temp, 3)) trans = Format(Mid(x, InStr(x, "-") + 1), "000") trans = temp & trans End Function
然后处理一下后面的数
判断左起是数字的位数开始位分出左边的数
数字位数定位format(a,"000")给字符 '定义固定3位
最后连接起来
Private Sub Command1_Click()
Dim strData(0 To 1) As String
Dim strTemp() As String
Dim i As Long, j As Long
strData(0) = "AVB4-1"
strData(1) = "AB5-36"
For i = 0 To UBound(strData)
strTemp = Split(strData(i), "-")
strTemp(1) = Format(strTemp(1), "000")
For j = Len(strTemp(0)) To 1 Step -1
If IsNumeric(Mid(strTemp(0), j, 1)) = False Then
strTemp(0) = Left(strTemp(0), j) & Format(Val(Right(strTemp(0), Len(strTemp(0)) - j)), "00")
Exit For
End If
Next
strData(i) = strTemp(0) & "-" & strTemp(1)
Debug.Print strData(i)
Next
End Sub
MsgBox trans("AB5-36") & vbCrLf & trans("AvB4-1")
End Sub
Function trans(ByVal x As String) As String
Dim temp As String
temp = Left(x, InStr(x, "-")) & " "
Mid(temp, Len(temp) - 2) = "0" & Trim(Right(temp, 3))
trans = Format(Mid(x, InStr(x, "-") + 1), "000")
trans = temp & trans
End Function
转换下面的就不行了:
MsgBox trans("AB15-36") & vbCrLf & trans("AvB14-11")
MsgBox trans("AB5-36") & vbCrLf & trans("AvB4-1")
End Sub
Function trans(ByVal x As String) As String
Dim a(2) As String
a(0) = Left(x, InStr(x, "-") - 1)
a(1) = IIf(IsNumeric(Right(a(0), 2)), Right(a(0), 2), Right(a(0), 1))
a(0) = Left(a(0), Len(a(0)) - Len(a(1)))
a(1) = Right("00" & a(1), 2) & "-"
a(2) = Right("000" & Mid(x, InStr(x, "-") + 1), 3)
trans = Join(a, "")
End Function