搜索宏未返回所有结果

时间:2019-07-26 21:24:30

标签: excel vba

您好,谢谢您的宝贵时间。

我有一个通过搜索电子表格数据填充的列表框。 列表框仅在最上面的行中填充我的预期退货。 列表框之后的第2列第2行的其余数据不会填充。

我在行号,列偏移量和添加更多的.listcounts中尝试了不同的偏移量。我尝试将最后一个listcount结尾数字更改为0,并在错误后正确填充了列表框,但仅填充了未知数量的行。

我可能是错的,但我想这与循环有关?

是否有更好的方法来完成此类任务? Data Range Capture Results Capture

Private Sub CommandButton1_Click()

Dim rng As Range    
Dim ws As Worksheet   
Dim i As Integer    
Set ws = ActiveSheet    
Set rng = ws.Range("A:E")    
Set fnd = rng.Find(TextBox1)    
If fnd Is Nothing Then MsgBox txtSearch & " not found": Exit Sub    
Set first = fnd   
With ListBox1    
.Clear    
.AddItem fnd.Offset(, -4)    
.List(.ListCount - 1, 1) = fnd.Offset(, 0)    
Do    
Set fnd = rng.FindNext(fnd)    
If fnd.Address = first.Address Then Exit Do    
.AddItem fnd.Offset(, -4)    
.List(.ListCount - 1, 1) = fnd.Offset(, 1)    
Loop    
End With  
End Sub 

3 个答案:

答案 0 :(得分:0)

您在这里没有循环。您的Do ...循环缺少While或直到语句。有关如何使用Do ... Loop语句的信息,请参见:https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/do-loop-statement。应该是这样的:

Private Sub CommandButton1_Click()

Dim rng As Range    
Dim ws As Worksheet   
Dim i As Integer    
Set ws = ActiveSheet    
Set rng = ws.Range("A:E")    
Set fnd = rng.Find(TextBox1)    
If fnd Is Nothing Then MsgBox txtSearch & " not found": Exit Sub    
Set first = fnd   
With ListBox1    
.Clear    
.AddItem fnd.Offset(, -4)    
.List(.ListCount - 1, 1) = fnd.Offset(, 0)    
Do    
Set fnd = rng.FindNext(fnd)    
If fnd.Address = first.Address Then Exit Do    
.AddItem fnd.Offset(, -4)    
.List(.ListCount - 1, 1) = fnd.Offset(, 1)    
Loop Until 'your logic here'
End With  
End Sub 

答案 1 :(得分:0)

尝试此宏

Option Explicit
Private Sub CommandButton1_Click()

Dim rng As Range
Dim ws As Worksheet
Dim adrs1$, Actual_adrs$
ListBox1.Clear
Set ws = ActiveSheet
Set rng = ws.Range("A:E")
Set fnd = rng.Find(TextBox1.Value)
If fnd Is Nothing Then MsgBox TextBox1.Value & " not found": Exit Sub
 Actual_adrs = fnd.Address: adrs1 = Actual_adrs

Do
    With ListBox1
    .AddItem fnd.Offset(, -4)
    .List(.ListCount - 1, 1) = fnd.Offset(, 0)
    End With

    Set fnd = rng.FindNext(fnd)
     Actual_adrs = fnd.Address
    If Actual_adrs = adrs1 Then Exit Do
Loop

End Sub

答案 2 :(得分:0)

正如评论所述,您的循环逻辑不正确。我认为这应该为您工作。我更改了间距,但是它几乎是同一宏,在循环开始时只有一个Do While

Private Sub CommandButton1_Click()
    Dim rng As Range, ws As Worksheet, i As Integer, first As Range
    Set ws = ActiveSheet
    Set rng = ws.Range("A:E")
    Set fnd = rng.Find(TextBox1)


    If fnd Is Nothing Then
        MsgBox txtSearch & " not found"
        Exit Sub
    End If

    Set first = fnd
    With ListBox1
        .Clear
        .AddItem fnd.Offset(, -4)
        .List(.ListCount - 1, 1) = fnd.Offset(, 0)

        'second find needs to be out of the loop
        Set fnd = rng.FindNext(fnd)

        Do While fnd.Address <> first.Address

            .AddItem fnd.Offset(, -4)
            .List(.ListCount - 1, 1) = fnd.Offset(, 1)

            Set fnd = rng.FindNext(fnd)
        Loop

    End With

End Sub