請問下面的VB不去sheet1能不能搞定。計劃使用表格搜索范圍內的多個單詞但不激活串列并留在作業表中。
這是代碼:
Sheet1.Activate
'search for searchText and populate list box with all data found
Dim searchText As String, FirstAddr As String
Dim FoundCell As Range, LastCell As Range, searchRange As Range
Dim i As Integer, endRow As Long
Dim foundTarget As Boolean
searchText = Me.txtTarget.Text
If Len(searchText) = 0 Then Exit Sub
Application.ScreenUpdating = False
Range("A1").End(xlDown).Select
endRow = ActiveCell.Row
Range("A1").Select
Application.ScreenUpdating = True
Set searchRange = Range("A2:D" & endRow)
Me.lstCustSearch.Clear
foundTarget = True
With searchRange
Set LastCell = .Cells(.Cells.Count)
End With
Set FoundCell = searchRange.Find(what:=searchText, after:=LastCell)
If Not FoundCell Is Nothing Then
FirstAddr = FoundCell.Address
Else
foundTarget = False
End If
i = 0
Do Until FoundCell Is Nothing
Me.lstCustSearch.AddItem Cells(FoundCell.Row, 1).Value
Me.lstCustSearch.List(i, 1) = Cells(FoundCell.Row, 2).Value
Me.lstCustSearch.List(i, 2) = Cells(FoundCell.Row, 3).Value
Me.lstCustSearch.List(i, 3) = _
Format(Cells(FoundCell.Row, 4).Value, "$#,##0.00")
Set FoundCell = searchRange.FindNext(after:=FoundCell)
If FoundCell.Address = FirstAddr Then
Exit Do
End If
i = i 1
Loop
If Not foundTarget Then
MsgBox "No data found for " & searchText
Else
Me.txtTarget.Text = ""
End If
Me.txtTarget.SetFocus
End Sub
另外,如果可能的話,創建一個命令按鈕,我可以在關閉表單后將所選資料填充到作業表中。
謝謝!
期望是在不激活串列的情況下使用表單。
uj5u.com熱心網友回復:
代替:
Sheet1.Activate
...
Range("A1").End(xlDown).Select
endRow = ActiveCell.Row
...你可以:
endrow = Sheet1.Range("A1").End(xlDown).Row
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/534676.html
標籤:擅长VBA
