我有大量信息,需要从中提取整数。
例如:
A1 : 130001
A2 : hello_24.02_75_150001
A3 : 650000_take:away
A4 : computer_800000_24.01.105
我想只取六位数字(130000,150001,650000,800000)。我怎么才能得到那些?
我尝试了IsNumeric(number)
和
If Regex.IsMatch(number, "^[0-9 ]+$") Then
...
End If
答案 0 :(得分:2)
您可以在VBA中使用用户定义的公式。将以下代码放在一个新模块中,并将其作为Excel中的函数调用为=Extract6Digits(A1)
Function Extract6Digits(Number As String)
Dim varCount As Integer
Dim i As Integer
Dim varOutput As String
Dim varTemp As String
varCount = 0
varTemp = ""
For i = 1 To Len("" & Number & "")
If Asc(Mid("" & Number & "", i, 1)) >= 48 And Asc(Mid("" & Number & "", i, 1)) <= 57 Then
varCount = varCount + 1
varTemp = varTemp & Mid("" & Number & "", i, 1)
Else
varCount = 0
varTemp = ""
End If
If varCount = 6 Then
If varOutput = "" Then
varOutput = varTemp
Else
varOutput = varOutput & "," & varTemp
End If
varCount = 0
varTemp = ""
End If
Next
Extract6Digits = varOutput
End Function
这绝不是最有效的方法,但它有效
答案 1 :(得分:2)
我以为你想要config.ListContainerData
解决方案;)
这是你想要的吗?
regexp
答案 2 :(得分:1)
试试这个
Sub test()
lastrow = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To lastrow
totallen = Len(Range("A" & i).Value)
For j = 1 To totallen
thischar = Mid(Range("A" & i), j, 1)
Select Case thischar
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "0"
If IsNumeric(Mid(Range("A" & i), j, 6)) = True Then
Range("B" & i).Value = Mid(Range("A" & i), j, 6)
Exit For
End If
End Select
Next j
Next i
End Sub
答案 3 :(得分:0)
尝试
Function GetSixDigits(s As String) As String
Dim intIndex As Integer
Dim strArr() As String
strArr = Split(s, "_")
For intIndex = LBound(strArr) To UBound(strArr)
If Len(strArr(intIndex)) = 6 And IsNumeric(strArr(intIndex)) Then
GetSixDigits = strArr(intIndex)
Exit Function
End If
Next
End Function
答案 4 :(得分:0)
扩展Siddharth Rout和AranDG的答案,我做了一些性能测试。
Siddharth Rout的解决方案测试
'----------------------------------------
'adapted from Siddharth Rout's answer
' works
Sub Sample()
Dim regEx, Match, matches
Dim rngRange As Range, cell As Range
Dim i As Integer
Dim mytime As Long
mytime = Timer
With CreateObject("vbscript.regexp")
.Pattern = "(^|\D)(\d{6})(\D|$)"
.Global = True
For Each cell In Range("A1:A12000").SpecialCells(xlCellTypeConstants, xlTextValues)
Set matches = .Execute(cell.Value)
If matches.Count > 0 Then
i = 0
For Each Match In matches
i = i + 1
cell.Offset(, i) = Match.SubMatches(1)
Next
End If
Next cell
End With
MsgBox Timer - mytime
End Sub
'----------------------------------------
AranDG解决方案的测试:
'----------------------------------------
'adapted form AranDG
' works
Sub Sample4()
Dim cell As Range
Dim arr As Variant
Dim mytime As Long
mytime = Timer
For Each cell In Range("A1:A12000").SpecialCells(xlCellTypeConstants, xlTextValues)
' cell.Offset(, 1) = Extract6Digits(cell.Value)
arr = Extract6Digits(cell.Value)
cell.Offset(, 1).Resize(, UBound(arr) + 1) = arr
Next cell
MsgBox Timer - mytime
End Sub
Function Extract6Digits(Number As String) As Variant
Dim varCount As Integer
Dim i As Integer
Dim varOutput As String
Dim varTemp As String
varCount = 0
varTemp = ""
For i = 1 To Len("" & Number & "")
If Asc(Mid("" & Number & "", i, 1)) >= 48 And Asc(Mid("" & Number & "", i, 1)) <= 57 Then
varCount = varCount + 1
varTemp = varTemp & Mid("" & Number & "", i, 1)
Else
varCount = 0
varTemp = ""
End If
If varCount = 6 Then
If varOutput = "" Then
varOutput = varTemp
Else
varOutput = varOutput & "," & varTemp
End If
varCount = 0
varTemp = ""
End If
Next
Extract6Digits = Split(varOutput, ",")
End Function
'----------------------------------------
我用活动的表格栏#34; A&#34;来自&#34; A1&#34;到&#34; A1000&#34;使用以下字符串:
hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105