我当前的解决方案是从特定列(AK)的单元格中删除所有非数字字符,我的代码需要7分钟才能运行360行。当我运行代码以“应用程序选择”选择所有360个单元时,只需10秒钟即可运行。理想情况下,我希望宏自己选择条件。请注意,数据是通过excel从查询中提取的。
我已经在网上搜索过,但是在代码自行选择该列的地方一无所获。我自己创建的代码需要7分钟而不是10秒。
下面的代码需要7分钟的时间才能运行,但是不需要用户选择数据。
Dim finRow As String
finRow = ActiveSheet.Range("A100000").End(xlUp).Row
Set myRange = ActiveSheet.Range("AK2:AK" & finRow)
For Each myCell In myRange
LastString = ""
For I = 1 To Len(myCell.Value)
mT = Mid(myCell.Value, I, 1)
If mT Like "[0-9]" Then
tString = mT
Else
tString = ""
End If
LastString = LastString & tString
Next I
myCell.Value = LastString
Next
下面的代码需要10秒钟,但是每次运行代码时,用户都必须选择条件。
Set myRange = Application.Selection
Set myRange = Application.InputBox("select one Range that you want to remove non numeric characters", "RemoveNonNum", myRange.Address, Type:=8)
For Each myCell In myRange
LastString = ""
For I = 1 To Len(myCell.Value)
mT = Mid(myCell.Value, I, 1)
If mT Like "[0-9]" Then
tString = mT
Else
tString = ""
End If
LastString = LastString & tString
Next I
myCell.Value = LastString
Next
当代码自行选择条件时,我希望输出为10秒。我感谢所有的帮助。谢谢你,马特
答案 0 :(得分:4)
使用变量数组并对其进行迭代。迭代范围非常耗时。
Dim finRow As Long
finRow = ActiveSheet.Range("A100000").End(xlUp).Row
Dim myRange() As Variant
myRange = ActiveSheet.Range("AK2:AK" & finRow)
Dim k As Long
For k = LBound(myRange, 1) To UBound(myRange, 1)
Dim lastSring As String
laststring = ""
Dim i As Long
For i = 1 To Len(myRange(k, 1))
Dim mT As String
mT = Mid(myRange(k, 1), i, 1)
If mT Like "[0-9]" Then
laststring = laststring & mT
End If
Next i
myRange(k, 1) = laststring
Next
ActiveSheet.Range("AK2:AK" & finRow).Value = myRange
答案 1 :(得分:1)
这会提高您的速度吗?:
time.dt.strftime('%Y-%m-%d %H:%M:%S')
答案 2 :(得分:0)
仅举一个示例,您也可以解决这个问题。完美吗?绝对不是,但这是一种有趣的处理方式:
之前:
Sub Test()
With ActiveWorkbook.Sheets("Blad1").Range("A1:A15").Cells
For X = 2 To 255
If IsNumeric(Chr(X)) = False And X <> 42 And X <> 43 And X <> 63 Then
.Replace what:=Chr(X), Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End If
Next X
End With
End Sub
之后: