【发布时间】:2017-06-30 13:00:25
【问题描述】:
我有一个包含大约 7000 条记录的列表,这些记录是带有数字的字符串。我需要提取所有数字,尤其是那些以“(”、“#”或“S”开头的数字。我们需要提取票号,以便分析每种类型存在多少问题票。下面是我写的代码。我认为一个数组可能会更好,但我无法弄清楚如何让它工作,所以现在,它循环遍历每一行中的每个字符,并且如果字符是数字,它开始复制并粘贴下一列中的数字,直到字符不再是数字。它将每组数字放在一个新列中,直到完成该行中的所有字符。
问题是它需要很长时间。上次我让它一路走下去,花了一个多小时。最近,我在进行更改时,我让它运行了大约 10 分钟然后停止它,它完成了大约 1200 条记录。每行最多可包含 100 个字符,但大多数接近 30 个。
有什么方法可以加快计算速度?
Sub findNumbers1()
Dim v As Integer, Length As Long, str As String, i As Long, r As Range,
lastRow As Long, nextCol, nextRow As Long, result, ArrayResult As String, ws
As Worksheet
nextRow = 0
nextCol = 0
Set ws = Worksheets("Sheet2")
lastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
Set r = ws.Range("A2:A6885")
nextRow = 1
For Each cell In r
str = cell.Value
Length = Len(str)
i = 1
nextCol = 2
nextRow = nextRow + 1
Do Until i > Length
If Mid(cell.Value, i, 1) = "(" Then
If IsNumeric(Mid(cell.Value, i + 1, 1)) Then
Do While IsNumeric(Mid(cell.Value, i + 1, 1))
result = Mid(cell.Value, i + 1, 1)
ArrayResult = ArrayResult + result
ws.Cells(nextRow, nextCol).Value = ArrayResult
i = i + 1
Loop
ArrayResult = ""
nextCol = nextCol + 1
Else
nextCol = ws.Cells(nextRow, Columns.Count).End(xlToLeft).Column + 1
End If
End If
If Mid(cell.Value, i, 1) = "#" Then
If IsNumeric(Mid(cell.Value, i + 1, 1)) Then
Do While IsNumeric(Mid(cell.Value, i + 1, 1))
result = Mid(cell.Value, i + 1, 1)
ArrayResult = ArrayResult + result
ws.Cells(nextRow, nextCol).Value = ArrayResult
i = i + 1
Loop
ArrayResult = ""
nextCol = nextCol + 1
Else
nextCol = ws.Cells(nextRow, Columns.Count).End(xlToLeft).Column + 1
End If
End If
If Mid(cell.Value, i, 1) = "S " Then
If IsNumeric(Mid(cell.Value, i + 1, 1)) Then
Do While IsNumeric(Mid(cell.Value, i + 1, 1))
result = Mid(cell.Value, i + 1, 1)
ArrayResult = ArrayResult + result
ws.Cells(nextRow, nextCol).Value = ArrayResult
i = i + 1
Loop
ArrayResult = ""
nextCol = nextCol + 1
Else
nextCol = ws.Cells(nextRow, Columns.Count).End(xlToLeft).Column + 1
End If
End If
i = i + 1
Loop
ArrayResult = ""
nextCol = nextCol + 1
Next cell
result = ""
ArrayResult = ""
Call pasteNoITMS
ws.ShowAllData
End Sub
Sub findNumbers2()
'pull all numbers from remaining applications after findnumbers1 runs.
Dim v As Integer, Length As Long, str As String, i As Long, r As Range, lastRow As Long, nextCol, nextRow As Long, result, ArrayResult As String, ws As Worksheet
result = ""
ArrayResult = ""
nextRow = 0
nextCol = 0
Set ws = Worksheets("2ndPull")
lastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
Set r = ws.Range("A2:A2000")
nextRow = 1
For Each cell In r
str = cell.Value
Length = Len(str)
i = 1
nextCol = 2
nextRow = nextRow + 1
ArrayResult = ""
Do Until i > Length
If IsNumeric(Mid(cell.Value, i + 1, 1)) Then
Do While IsNumeric(Mid(cell.Value, i + 1, 1))
result = Mid(cell.Value, i + 1, 1)
ArrayResult = ArrayResult + result
ws.Cells(nextRow, nextCol).Value = ArrayResult
i = i + 1
Loop
ArrayResult = ""
nextCol = nextCol + 1
Else
nextCol = ws.Cells(nextRow, Columns.Count).End(xlToLeft).Column + 1
End If
i = i + 1
Loop
ArrayResult = ""
nextCol = nextCol + 1
Next cell
result = ""
ArrayResult = ""
Call sortPulled
End Sub
【问题讨论】:
-
位数是否一致(即始终为 7 位)?如果是这样,正则表达式将为您完成这项任务。如果不是,请提供有关数据外观的更多详细信息。
-
如果您的代码已经正常运行,最好将这个问题发布到代码审查社区。有关一般提示,请查看this link。
-
尝试切换screen updating
-
谢谢大家。 @BerticusMaximus,我很欣赏评论社区的提示。我不经常使用 SO,但我下次会这样做。
-
请参阅下面的一些示例数据:VSDN (22542) - NA 和移动 (23468) SMART - Material Trigger (SMART) (11417) Smart (19221), vid(20703), (12569) , 驱动器 (20184), 数据 (13742)
标签: string vba excel numbers information-extraction