2015-07-03 49 views
0

我的代碼完美地將過濾的數據返回到表單上的三列文本框。當我在調試中通過它時,我可以看到執行我的新手(但工作)代碼需要很長時間。我記錄了宏,試圖瞭解如何使用AutoFilter腳本來做同樣的事情。因此,腳本有更快的速度,但我很害怕被打敗。任何人都可以告訴我怎麼做到這一點,或者我是否應該這麼做?帶有自動篩選器的Shortern代碼

Private Sub CommandButton1_Click() 
Dim i As Integer, c As Integer, d As Integer 
Dim e As Integer, f As Integer, g As Integer 
Dim lstrw As Integer, ws As Worksheet 
Dim MySerNum As String, MyLocation As String 
Dim MySearchValue As String, MyStatus As String 

    MySearchValue = "x" 'search column for cells that contain only the letter "x" 
    Set ws = Sheets(2) 
         'The user will type an "x" in column N at the rows... 
         'he wishes to return data from to the form TextBoxes 
    lstrw = ws.Range("N" & Rows.Count).End(xlUp).Row 
         'find how many cells contain string "x" 
    For f = 1 To lstrw 
     If ws.Cells(f, 14).Value = MySearchValue Then 
      e = e + 1 
     End If 
    Next f 
         ' loop to find occurrence of "x" and load variables 
    For i = 1 To lstrw 
         'the adjacent textbox on the form to TextBox1... 
         'is TextBox16 hence the +15 below 
     If ws.Cells(i, 14).Value = MySearchValue Then 
      MySerNum = ws.Cells(i, 2).Value 
     End If 
     If ws.Cells(i, 14).Value = MySearchValue Then 
      MyLocation = ws.Cells(i, 4).Value 
     End If 
     If ws.Cells(i, 14).Value = MySearchValue Then 
      MyStatus = ws.Cells(i, 5).Value 
     End If 
     d = c + 15 
     g = d + 15 
     Me.Controls("TextBox" & c).Value = MySerNum 
     Me.Controls("TextBox" & d).Value = MyLocation 
     Me.Controls("TextBox" & g).Value = MyStatus 
     If ws.Cells(i, 14).Value = MySearchValue Then 
      c = c + 1 
     End If 
    Next i 
End Sub 
+0

由於您的代碼正常工作,但您希望提高性能,所以這將更適合[代碼評論](http://codereview.stackexchange.com/)。 StackOverflow適用於破損的代碼。 – FreeMan

+0

謝謝FreeMan。我不知道那個論壇。 –

+0

沒問題,很多人都沒有。 – FreeMan

回答

1

那說 - 這裏有幾個技巧:

變化:

For i = 1 To lstrw 
        'the adjacent textbox on the form to TextBox1... 
        'is TextBox16 hence the +15 below 
    If ws.Cells(i, 14).Value = MySearchValue Then 
     MySerNum = ws.Cells(i, 2).Value 
    End If 
    If ws.Cells(i, 14).Value = MySearchValue Then 
     MyLocation = ws.Cells(i, 4).Value 
    End If 
    If ws.Cells(i, 14).Value = MySearchValue Then 
     MyStatus = ws.Cells(i, 5).Value 
    End If 
    d = c + 15 
    g = d + 15 
    Me.Controls("TextBox" & c).Value = MySerNum 
    Me.Controls("TextBox" & d).Value = MyLocation 
    Me.Controls("TextBox" & g).Value = MyStatus 
    If ws.Cells(i, 14).Value = MySearchValue Then 
     c = c + 1 
    End If 
Next i 

要:

For i = 1 To lstrw 
        'the adjacent textbox on the form to TextBox1... 
        'is TextBox16 hence the +15 below 
    If ws.Cells(i, 14).Value = MySearchValue Then 
     Me.Controls("TextBox" & c).Value = ws.Cells(i, 2).Value 
     d = c + 15 
     Me.Controls("TextBox" & d).Value = ws.Cells(i, 4).Value 
     g = d + 15 
     Me.Controls("TextBox" & g).Value = ws.Cells(i, 5).Value 
     c = c + 1 
    End If 
Next i 

這樣做會刪除所有的中間My...變量,所以你可以刪除他們的Dim報表。如果您稍後需要它們,則可以在循環結束時將它們設置一次。這會加快一點。

+0

謝謝你的提示FreeMan。非常感謝你。 –