how to Speed Up the VBA Macros

前端 未结 7 1665
陌清茗
陌清茗 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:11

    Get hold of a copy of Professional Excel Development which includes an excellent profiling utility called PerfMon. It will allow you to see which parts of the report are taking all the time so you can analyse and rewrite

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-24 06:14

    Surely you mean

    Application.ScreenUpdating = False
    

    Apart from that you could also look to disable the recalculation of the workbook whilst the macro is running and see if that makes a difference. This is of course assuming that the bottle neck is with the spreadsheet part of the process, if its taking ages to get the data from access that might be an area to look at

    0 讨论(0)
  • 2021-01-24 06:14

    Yes, make a table in Access to hold your client IDs. Then create the query here and connect to it with the external data connector. After that refresh it manually or use VBA to refresh the connection whenever you're ready.

    0 讨论(0)
  • 2021-01-24 06:26

    Take a look at Chris comments. We believe that your performance bottleneck is likely to be in the way you're querying the database rather than in the VBA code that applies the data into the sheet.

    Simple questions about Access performance: - Your tables have indexes? - Are you using any kind of table join? - Are the Access databases local on your computer or being accessed remotely?

    Again, I'm only reinforcing what Chris already commented.

    0 讨论(0)
  • 2021-01-24 06:28

    I'd try to do MORE of the work on the database side. Generate the reports you want on the database side, and then export the results to Excel.

    Access is MUCH better at automating reports than Excel is.

    0 讨论(0)
提交回复
热议问题