如何删除句子中间不同数字的年份

时间:2017-10-15 21:54:13

标签: excel excel-vba function vba

在Excel中,对于以下数据,我希望删除不同数字的年份:

Honda Motorcycle 1983 XR350R A CAMSHAFT + VALVE

Honda ATV 1999 TRX400EX A - FOURTRAX 400EX CAMSHAFT + VALVE

此外,我希望数据看起来如下:

本田摩托车XR350R凸轮轴+阀门

本田ATV TRX400EX A - FOURTRAX 400EX凸轮轴+阀门

1 个答案:

答案 0 :(得分:1)

对于 A 列中的数据,这将删除1900年到2020年之间的年份:

Sub YearKiller()
    Dim r As Range, rng As Range
    Set rng = Columns(1).SpecialCells(2)
    For Each r In rng
        msg = ""
        v = r.Value
        If InStr(1, v, " ") <> 0 Then
            ary = Split(v, " ")
            For i = LBound(ary) To UBound(ary)
                a = ary(i)
                If Len(a) = 4 And IsNumeric(a) Then
                    n = CDbl(a)
                    If n > 1900 And n < 2020 Then
                        ary(i) = " "
                    End If
                End If
            Next i
            r.Value = Application.WorksheetFunction.Trim(Join(ary, " "))
        End If
    Next r
End Sub