优化VBA程序

时间:2018-06-28 15:00:51

标签: excel vba excel-vba

我创建了几个VBA函数和宏来使我的工作自动化,但是随着越来越多的数据输入,我注意到运行宏的延迟更大。我是否可以更改或更改代码以提高其效率?

该程序的前提: -刷新按钮可循环浏览所有工作表,根据其完成情况更改颜色,并将“未完成/过期”表格的信息放入表格中(最慢)

'===============
'Refresh Button on MASTER PAGE
'Functions: Updates color of sheets, based on completion/incompletion
'           Removes inputs from MASTER page
'           Updates Expired Forms cells
'====================
Sub refresh_form()
Dim ws As Worksheet
Dim wb As Workbook
Dim wsMASTER, wsTEMP
Dim complete, incomplete, exp, default   'to store color index's
Dim expName, expDate, expGSA, expStatus  'to store values for expired forms
Dim lastRow As Long                      'to store row # for expired & incomplete form

'CLEARS DATA FROM MAIN SHEET
ThisWorkbook.Worksheets("MASTER").Range("C6").Value = ""    'Project name
ThisWorkbook.Worksheets("MASTER").Range("C7").Value = ""    'Address
ThisWorkbook.Worksheets("MASTER").Range("C8").Value = ""    'Date
ThisWorkbook.Worksheets("MASTER").Range("C9").Value = ""    'GSA #
ThisWorkbook.Worksheets("MASTER").Range("C10").Value = ""   'Exp Date

wsMASTER = "MASTER"             'Sets wsMASTER as MASTER worksheet
wsTEMP = "TEMPLATE"             'Sets wsTEMP as TEMPLATE worksheet

complete = 4        'Green
incomplete = 44     'Orange
default = 2         'White
exp = 3             'Red

lastRow = 5        'Expired & Incomplete row starts at 5

For Each ws In ThisWorkbook.Worksheets                                                'Loops through all worksheets on click
    If ws.Name = wsMASTER Or ws.Name = wsTEMP Then                                      'For MASTER and TEMPLATE sheet, skip
        ws.Tab.ColorIndex = default
    ElseIf ws.Range("$M12").Value = True And ws.Range("$M$15").Value = True Then        'Applies "Exp" tab color to expired/incomp forms
        ws.Tab.ColorIndex = exp
        expName = ws.Range("$C$5").Value    'Stores current form's project name
        expDate = ws.Range("$C$9").Value    '***expiration date
        expGSA = ws.Range("$C$8").Value     '***GSA number
        lastRow = lastRow + 1               'increments lastRow by a value of 1
                                                                                        'VALUES INPUTTED IN EXPIRED & INCOMPLETE FORM
        ThisWorkbook.Worksheets("MASTER").Range("K" & lastRow).Value = expGSA           '       GSA #
        ThisWorkbook.Worksheets("MASTER").Range("L" & lastRow).Value = expName          '       Project name
        ThisWorkbook.Worksheets("MASTER").Range("M" & lastRow).Value = expDate          '       Expiration date

    ElseIf ws.Range("$M$12").Value = True Then                                          'Applies "Incomplete" tab color to incomplete forms
        ws.Tab.ColorIndex = incomplete
    ElseIf ws.Range("$M$12").Value = False And ws.Range("$N$12").Value = True Then      'Applies "Complete" tab color to complete forms
        ws.Tab.ColorIndex = complete
    Else                                                                                'Applies "Default" tab color to any untouched forms
        ws.Tab.ColorIndex = default
    End If
Next ws                                                                               'End Loop

End Sub                                                                                   'End Sub

3 个答案:

答案 0 :(得分:4)

最好在Code Review回答这个问题,但是提高性能的一种简单方法是执行以下操作:

'===============
'Refresh Button on MASTER PAGE
'Functions: Updates color of sheets, based on completion/incompletion
'           Removes inputs from MASTER page
'           Updates Expired Forms cells
'====================
Sub refresh_form()
Dim ws As Worksheet
Dim wsMaster As Worksheet: Set wsMaster = Worksheets("MASTER")
Dim wb As Workbook
Dim wsTEMP As String
Dim complete As Integer, incomplete As Integer, exp As Integer, default As Integer       'to store color index's
Dim lastRow As Long                      'to store row # for expired & incomplete form

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual

'CLEARS DATA FROM MAIN SHEET
 wsMaster.Range("C6:C10").ClearContents

complete = 4        'Green
incomplete = 44     'Orange
default = 2         'White
exp = 3             'Red
lastRow = 5        'Expired & Incomplete row starts at 5

For Each ws In ThisWorkbook.Worksheets                                                'Loops through all worksheets on click
    If ws.Name = wsMaster.Name Or ws.Name = "TEMPLATE" Then                           'For MASTER and TEMPLATE sheet, skip
        ws.Tab.ColorIndex = default
    ElseIf ws.Range("$M12").Value = True And ws.Range("$M$15").Value = True Then      'Applies "Exp" tab color to expired/incomp forms
        ws.Tab.ColorIndex = exp
        lastRow = lastRow + 1                                                         'increments lastRow by a value of 1
        wsMaster.Range("K" & lastRow).Value = ws.Range("$C$8").Value                  'GSA #
        wsMaster.Range("L" & lastRow).Value = ws.Range("$C$5").Value                  'Project name
        wsMaster.Range("M" & lastRow).Value = ws.Range("$C$9").Value                  'Expiration date
    ElseIf ws.Range("$M$12").Value = True Then                                        'Applies "Incomplete" tab color to incomplete forms
        ws.Tab.ColorIndex = incomplete
    ElseIf ws.Range("$M$12").Value = False And ws.Range("$N$12").Value = True Then    'Applies "Complete" tab color to complete forms
        ws.Tab.ColorIndex = complete
    Else                                                                              'Applies "Default" tab color to any untouched forms
        ws.Tab.ColorIndex = default
    End If
Next ws                                                                               'End Loop

Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

答案 1 :(得分:1)

将其添加到DIM下方的代码开头

Application.calculation=xlcalculationmanual
application.screenupdating=false
application.displaystatusbar=false
application.enableevents=false

然后将其添加到代码末尾的子

之前
Application.calculation=xlcalculationautomatic
application.screenupdating=true
application.displaystatusbar=true
application.enableevents=true

这应该有助于加快代码的速度。

答案 2 :(得分:1)

您的大多数宏都没有做非常密集的事情。 Excel在工作表之间进行切换时正在执行的最密集的操作是更新UI。如果您暂时禁用UI更新,则可能会看到明显的进步。

在进入For Each循环之前,请致电 Application.ScreenUpdating = False

在退出子例程之前,请恢复屏幕更新 Application.ScreenUpdating = True

您可以采取许多其他措施来提高代码的性能。其他优化选项是将工作表的数量保持在最低水平,或者使用多个工作簿。