-3

As per title, the codes below are located in my Private Sub Workbook_open(). Therefore every time I open my workbook it is slow. How do I get to optimize the code to run fastest? Any help is appreciated.

'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
4

1 に答える 1

1

It looks like your loop is iterating through every row or every column on a worksheet. Instead of using the last row or last column try using the last used row or last used column. This way instead of moving through thousands of blank rows you only check rows containing data.

Try:

'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
于 2013-02-25T16:12:17.220 に答える