【问题标题】:Other option for 'ActiveCell'\'ActiveCell\' 的其他选项
【发布时间】:2022-11-16 02:59:54
【问题描述】:

请问下面的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

另外,如果可能的话,创建一个命令按钮,我可以在关闭表单后将所选数据填充到工作表中。

谢谢!

期望是在不激活列表的情况下使用表单。

【问题讨论】:

标签: excel vba


【解决方案1】:

代替:

Sheet1.Activate
...
Range("A1").End(xlDown).Select
endRow = ActiveCell.Row

...你可以:

endrow = Sheet1.Range("A1").End(xlDown).Row

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多