Sub ExtractTheLastSuccessiveNumbers() 'Update by 20150702 Dim Rng As Range Dim WorkRng As Range Dim xValue AsString Dim OutValue AsString Dim PreviousIsNumber AsBoolean Dim Find AsBoolean Dim index AsInteger
OnErrorResumeNext xTitleId = "QQ的小工具箱[逆序读取]" Set WorkRng = Application.Selection Set WorkRng = Application.InputBox("选择数据范围", xTitleId, WorkRng.Address, Type:=8)
ForEach Rng In WorkRng OutValue = "" PreviousIsNumber = False Find = False xValue = Rng.Value index = VBA.Len(xValue) + 1 For i = 1To VBA.Len(xValue)
If VBA.IsNumeric(VBA.Mid(xValue, index - i, 1)) Then Find = True If PreviousIsNumber Then OutValue = VBA.Mid(xValue, index - i, 1) & OutValue Else OutValue = VBA.Mid(xValue, index - i, 1) EndIf PreviousIsNumber = True Else PreviousIsNumber = False If Find Then ExitFor EndIf EndIf Next Rng.NumberFormat = "@" 'Rng.NumberFormat = "0" If VBA.Len(OutValue) = 15Or VBA.Len(OutValue) = 16Or VBA.Len(OutValue) = 17Then Rng.Value = OutValue Else Rng.Value = "" EndIf Next
EndSub
将 xxx.bas 文件导入到 Excel
在 Excel 中按住 Alt + F11 打开 Microsoft Visual Basic Applications 程序,然后将 xxx.bas 文件导入。