how to Speed Up the VBA Macros

前端 未结 7 1893
陌清茗
陌清茗 2021-01-24 06:06

I am Generating a New Sheets using macros. For a New Sheet generation , Data is retrieved from more than 4 MS Access DB. Each DB had minimum 200 field. My Macro code includes

7条回答
  •  感动是毒
    2021-01-24 06:13

    There is some disucussion of this topic here.

    Edit: Ok, then the next step is to identify which parts of your code are taking the longest. The simplest way to do this is to make a copy of your code and just start measuring various parts like this:

    Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
    Private mlngStrt As Long
    Private mlngEnd As Long
    
    Private Const u As Long = 10000000
    
    Public Sub Example()
        Dim i As Long
    
        mlngStrt = GetTickCount
        For i = 0 To u
        Next
        mlngEnd = GetTickCount
        Debug.Print "Section1", mlngEnd - mlngStrt
    
        mlngStrt = GetTickCount
        ExampleSubCall
        mlngEnd = GetTickCount
        Debug.Print "ExampleSubCall", mlngEnd - mlngStrt
    
        mlngStrt = GetTickCount
        For i = 0 To (u * 1.5)
        Next
        mlngEnd = GetTickCount
        Debug.Print "Section2", mlngEnd - mlngStrt
        Debug.Print "Example Complete"
    End Sub
    
    Private Sub ExampleSubCall()
        Dim i As Long
        For i = 0 To (u * 0.75)
        Next
    End Sub
    

    This approach is fairly straight-forward. The drawback here is that you need to insert all of the timing statements and then turn around and remove them. Which is why I would work on a copy.

    Once you know what parts are taking the longest you know where to focus your attention and what to ask for help with.

提交回复
热议问题