2013-02-25 87 views
-3

按照標題,以下代碼位於我的Private Sub Workbook_open()中。因此,每次打開工作簿時都很慢。如何優化代碼以最快運行?任何幫助表示讚賞。EXCEL VBA加快我的代碼

'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 
+0

實現代碼評審,現場http://codereview.stackexchange.com/可以幫助你比這 – SeanC 2013-02-25 16:15:05

回答

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 
+0

咱們倒不如說我在被佔領排之間有空白行。它還會檢查下一個佔用的行嗎?你明白我的意思嗎? – 2013-02-25 16:22:54

+0

它將檢查下一個佔用的行。 – Ripster 2013-02-25 16:25:05

+0

它會檢查多少行?如果我空白2行將檢查2行後? – 2013-02-25 16:43:13