EXCEL VBA加速我的代码

时间:2013-02-25 15:37:22

标签: excel vba

根据标题,以下代码位于我的Private Sub Workbook_open()。因此,每次打开我的工作簿时都会很慢。如何优化代码以最快地运行?任何帮助表示赞赏。

'Sheets("Summary(FG)") ComboBox1 items
For b = 3 To Sheets("CustomerList").Cells(3, 2).SpecialCells(xlLastCell).row
    If Sheets("CustomerList").Cells(b, 2) <> "" Then
        Worksheets("Summary(FG)").ComboBox1.AddItem (Sheets("CustomerList").Cells(b, 2))
    Else
    End If
Next

'Sheets("Summary(RawMat)") ComboBox1 items
For a = 2 To Sheets("RawMatList").Cells(2, 2).SpecialCells(xlLastCell).Column
    If Sheets("RawMatList").Cells(2, a) <> "" Then
        Worksheets("Summary(RawMat)").ComboBox1.AddItem (Sheets("RawMatList").Cells(2, a))
    End If
Next

'sheets("Summary(WIP)") ComboBox1 items
For c = 3 To Sheets("WIPList").Cells(3, 2).SpecialCells(xlLastCell).row
    If Sheets("WIPList").Cells(c, 2) <> "" Then
        Worksheets("Summary(WIP)").ComboBox1.AddItem (Sheets("WIPList").Cells(c, 2))
    End If
Next

For Each Worksheet In Worksheets
    Application.Goto Reference:=Range("A1"), Scroll:=True
Next Worksheet

1 个答案:

答案 0 :(得分:1)

看起来你的循环遍历工作表上的每一行或每一列。而不是使用最后一行或最后一列尝试使用上次使用的行或上次使用的列。这样,您只需检查包含数据的行,而不是移动数千个空白行。

尝试:

'Sheets("Summary(FG)") ComboBox1 items
For b = 3 To Sheets("CustomerList").UsedRange.Rows.Count
    If Sheets("CustomerList").Cells(b, 2) <> "" Then
        Worksheets("Summary(FG)").ComboBox1.AddItem (Sheets("CustomerList").Cells(b, 2))
    Else
    End If
Next

'Sheets("Summary(RawMat)") ComboBox1 items
For a = 2 To Sheets("RawMatList").UsedRange.Columns.Count
    If Sheets("RawMatList").Cells(2, a) <> "" Then
        Worksheets("Summary(RawMat)").ComboBox1.AddItem (Sheets("RawMatList").Cells(2, a))
    End If
Next

'sheets("Summary(WIP)") ComboBox1 items
For c = 3 To Sheets("WIPList").UsedRange.Rows.Count
    If Sheets("WIPList").Cells(c, 2) <> "" Then
        Worksheets("Summary(WIP)").ComboBox1.AddItem (Sheets("WIPList").Cells(c, 2))
    End If
Next

For Each Worksheet In Worksheets
    Application.Goto Reference:=Range("A1"), Scroll:=True
Next Worksheet