4

我正在使用“XXXX-000-000-000”格式的单元格值。

  • 第一个连字符之前的所有内容都需要删除,我可以这样做。
  • 字符串的其余部分需要减少为整数,并删除连字符和任何多余的零。

我无法将零点保持在正确的位置。

  • AD12-002-020-34 需要看起来像这样:2-20-34
  • CA1-002-101-001 需要看起来像这样:2-101-1
  • AD12-002-020-10 需要看起来像这样:2-20-10

例如:

dim ir as range

ir = "AD12-002-020-100"

ir1 = InStr(ir, "-")
ir2 = InStrRev(ir, "-")
ir.Offset(0, 1) = Mid(ir, ir1 + 1, ir2 - ir1 + 3)

这给了我:002-020-100

建议?先感谢您!

4

7 回答 7

15

考虑:

Sub dural()
    Dim s As String
    s = "AD12-002-020-34"
    s = Replace(s, "-0", "-")
    s = Replace(s, "-0", "-")
    ary = Split(s, "-")
    ary(0) = ""
    s = Mid(Join(ary, "-"), 2)
    MsgBox s
End Sub
于 2013-09-26T14:36:09.383 回答
4

此外,这将用作 UDF(用户定义的函数)

Function STRIP(r As String)
    If InStr(1, r, "-00", vbTextCompare) Then
        r = Replace(r, "-00", "-")
    End If
    If InStr(1, r, "-0", vbTextCompare) Then
        r = Replace(r, "-0", "-")
    End If
    Dim v As Variant, s As String, i As Long
    v = Split(r, "-")
    For i = 1 To UBound(v)
        s = s & "-" & v(i)
    Next i
    STRIP = Right(s, Len(s) - 1)
End Function

您只需从任何单元格调用它,=STRIP(A1)其中 whereA1是对您要拆分的任何单元格的引用

例子:

在此处输入图像描述

于 2013-09-26T15:08:38.373 回答
3

UDF 不是必需的(但显然是一个更好的主意!):

=LEFT(MID(A1,FIND("-",A1)+1,LEN(A1)),FIND("-",MID(A1,FIND("-",A1)+1,LEN(A1)))-1)*1&"-"&VALUE(MID(MID(A1,FIND("-",A1)+1,LEN(A1)),FIND("-",MID(A1,FIND("-",A1)+1,LEN(A1)))+1,FIND("-",MID(A1,FIND("-",A1)+1,LEN(A1)),FIND("-",MID(A1,FIND("-",A1)+1,LEN(A1)))+1)-FIND("-",MID(A1,FIND("-",A1)+1,LEN(A1)))-1))&"-"&VALUE(MID(MID(A1,FIND("-",A1)+1,LEN(A1)),FIND("-",MID(A1,FIND("-",A1)+1,LEN(A1)),FIND("-",MID(A1,FIND("-",A1)+1,LEN(A1)))+1)+1,LEN(A1)))

于 2013-09-26T14:51:15.863 回答
3

方法

Sub TestCleanNames()
   Debug.Print CleanString("AD12-002-020-34")
   Debug.Print CleanString("CA1-002-101-001")
   Debug.Print CleanString("AD12-002-020-10")
End Sub

Function CleanString(strIn As String) As String
    Dim objRegex As Object
    Set objRegex = CreateObject("vbscript.regexp")
    With objRegex
      'remove first portion
      .Pattern = "(.+?)-"
      CleanString = .Replace(strIn, vbNullString)
      .Global = True
      'trim leasfing zeroes
      .Pattern = "(\-|^)(0)+([1-9])"
      CleanString = .Replace(CleanString, "$1$3")
    End With
End Function
于 2013-10-11T14:34:13.030 回答
3

我也可以玩吗?:p

一班轮

Debug.Print Mid(Replace(Replace(Replace(Mid(sString, InStr(1, sString, "-")), "-000", "-"), "-00", "-"), "-0", "-"), 2)

sString有效"XXXX-000-000-000"字符串在哪里

在此处输入图像描述

于 2013-10-12T10:33:23.020 回答
1
Sub x()    
    'Technically, you don't have to declare variables, but it prevents typos:
    Dim ir As String
    Dim ir1 As String
    Dim ir2 As String
    Dim ir3 As String
    ir = "AD12-002-020-100"

    'First, get rid of the first 5 characters:
    ir = Right(ir, Len(ir) - 5)

    'Isolate each section. Convert to Int to get rid of leading zeros:
    ir1 = CInt(Left(ir, 3))
    ir2 = CInt(Right(Left(ir, 7), 3))
    ir3 = CInt(Right(ir, 3))

    'Return the result:
    MsgBox ir1 & "-" & ir2 & "-" & ir3
End Sub
于 2013-09-26T14:40:28.280 回答
1

以为我会把我的帽子扔在这个戒指上。请注意,所有版本都假设要转换的单元格位于从单元格 A1 开始的 A 列中。

作为一个宏:

Sub tgr()

    Dim arrResults() As String
    Dim varText As Variant
    Dim varPart As Variant
    Dim ResultIndex As Long

    With Range("A1", Cells(Rows.Count, "A").End(xlUp))
        ReDim arrResults(1 To .Rows.Count, 1 To 1)
        For Each varText In .Value
            ResultIndex = ResultIndex + 1
            For Each varPart In Split(Mid(varText, InStr(varText, "-") + 1), "-")
                arrResults(ResultIndex, 1) = arrResults(ResultIndex, 1) & "-" & Val(varPart)
            Next varPart
            arrResults(ResultIndex, 1) = Mid(arrResults(ResultIndex, 1), 2)
        Next varText
        .Value = arrResults
    End With

End Sub

作为 UDF:

Function tgrUDF(sText As String) As String

    Dim varPart As Variant

    For Each varPart In Split(Mid(sText, InStr(sText, "-") + 1), "-")
        tgrUDF = tgrUDF & "-" & Val(varPart)
    Next varPart

    tgrUDF = Mid(tgrUDF, 2)

End Function

作为一个公式:

=--MID(SUBSTITUTE(A1,"-",REPT(" ",99)),99,99)&"-"&--MID(SUBSTITUTE(A1,"-",REPT(" ",99) ),99*2,99)&"-"&--MID(SUBSTITUTE(A1,"-",REPT(" ",99)),99*3,99)

于 2013-09-26T15:58:55.597 回答