0

我需要关闭第一行,所以我将 Sub 中的范围从

For Each rng In Sheets("360").Range("B:CJ" & Lastrow)

For Each rng In Sheets("360").Range("B2:CJ2" & Lastrow)

子知道大麦运行它需要 5 秒才能运行并且知道即使它完成也需要几分钟

如何解决这个问题?

谢谢

Sub CleanAll()

    Dim rng As Range
    Dim Lastrow As Long

    With Sheets("360")
        Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
    End With

    For Each rng In Sheets("360").Range("B2:CJ" & Lastrow)
        rng.Value = NumberOnly(rng.Value)
    Next
End Sub

功能

Function NumberOnly(strSource As String) As String
Dim i As Integer
Dim strResult As String

For i = 1 To Len(strSource)
    Select Case Asc(Mid(strSource, i, 1))
        Case 32, 48 To 57, 65, 78:
            strResult = strResult & Mid(strSource, i, 1)
    End Select
Next
NumberOnly = strResult

End Function
4

2 回答 2

4
For Each rng In Sheets("360").Range("B2:CJ2" & Lastrow)

像这样删除以下 2 个 CJ:

For Each rng In Sheets("360").Range("B2:CJ" & Lastrow)

Lastrow 已经指定了最后一行的编号,所以如果 Lastrow 的值为 300 并且您编写“CJ2”和 Lastrow,那么您的实际范围现在是 B2:CJ2300,这意味着您现在要运行 2000 多行。

于 2013-08-14T14:35:05.340 回答
1

我发现在表格中使用您的数据将解决很多重新调整大小范围的问题。

话虽如此,您应该从您的范围创建一个变体数组,然后循环遍历该变体数组。一旦完成复制到数组回到范围。这将加速它一百倍。

像这样的东西...

Sub CleanAll()

    Dim myArray As Variant
    Dim Lastrow As Long


    With Sheets("360")
        Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
    End With

    myArray = Sheets("360").Range("B2:CJ" & Lastrow)

    For x = LBound(myArray) To UBound(myArray)
        For y = LBound(myArray, 2) To UBound(myArray, 2)
            myArray(x, y) = NumberOnly(myArray(x, y))
        Next y
    Next x

    Sheets("360").Range("B2:CJ" & Lastrow) = myArray
End Sub

Function NumberOnly(ByVal strSource As String) As String
    Dim i As Integer
    Dim strResult As String

    For i = 1 To Len(strSource)
        Select Case Asc(Mid(strSource, i, 1))
            Case 32, 48 To 57, 65, 78:
                strResult = strResult & Mid(strSource, i, 1)
        End Select
    Next
    NumberOnly = strResult

End Function
于 2013-08-14T15:47:09.263 回答