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
由於您的代碼正常工作,但您希望提高性能,所以這將更適合[代碼評論](http://codereview.stackexchange.com/)。 StackOverflow適用於破損的代碼。 – FreeMan
謝謝FreeMan。我不知道那個論壇。 –
沒問題,很多人都沒有。 – FreeMan