循环功能太慢了

时间:2014-05-07 18:27:25

标签: excel-vba vba excel

在我每天运行的一份报告中,我必须从网站上提取数据。该网站提供了选择"导出到excel"。导出的问题是它将时间格式化为文本。因此,如果不修改这些单元格的值,我就无法对该数据执行任何操作。我必须添加一个" 0:"面对细胞的价值使其发挥作用。从那里我可以将单元格(自定义)格式化为" mm:ss"。

现在我正在使用一个子(如下所示)循环遍历每个单元格,搜索" ??:?? ??#34;并将该单元格的值替换为Activecell =" 0:" &安培; Activecell.Value。这很有效。

问题是,它是STUPID缓慢,像令人难以置信的痛苦缓慢大声笑,我平均有200到300个条目需要修复。每次进入大约需要40秒。关于主题,有趣的是,如果我使用screen.updating = true来运行它自己的sub,它实际上运行得更快。

无论如何,这是目前使用的代码:

    Sub sub_Add0toTime()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''  Convert exported time to mm:ss by adding 0: to the front of each cell
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Dim FindString As String
    Dim rng As Range
    FindString = "??:??"
50
    If Trim(FindString) <> "" Then
        With Sheets("CAHT RAW").Range("F:F")
            Set rng = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not rng Is Nothing Then
                Application.Goto rng, True
                ActiveCell.Value = "0:" & ActiveCell.Value
                Do
                Set rng = .FindNext(rng)
                GoTo 50
                Loop While Not rng Is Nothing
                Else
            End If
        End With
    End If
    Range("A1").Select
End Sub

我需要简化这一点,以便它运行得更快,就像其他人一样,我对语言的了解有限。

感谢您的帮助。

xXCDXx

ANSWERED:

所以,在我从午餐回来之后,我尝试在宏运行之前关闭应用程序自动计算器,然后在之后重新开启。它就像一个魅力。我不认为我曾经有太多关于其他数据的数据,这是一个显着的影响。以下是最终的代码:

Sub sub_Add0toTime()

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''''  Convert exported time to mm:ss by adding 0: to the front of each cell
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Application.Calculation = xlCalculationManual

        Dim FindString As String
        Dim rng As Range
        FindString = "??:??"
    50
        If Trim(FindString) <> "" Then
            With Sheets("CAHT RAW").Range("F:F")
                Set rng = .Find(What:=FindString, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                If Not rng Is Nothing Then
                    Application.Goto rng, True
                    ActiveCell.Value = "0:" & ActiveCell.Value
                    Do
                    Set rng = .FindNext(rng)
                    GoTo 50
                    Loop While Not rng Is Nothing
                    Else
                End If
            End With
        End If
        Range("A1").Select

Application.Calculation = xlCalculationAutomatic

End Sub

1 个答案:

答案 0 :(得分:0)

假设您的数据只是几小时和几分钟,为什么不这样做?

With Range("F:F")
    .NumberFormat = "[h]:mm"
    .Value = .Value
End With