结束.FindNext包装而没有循环?

时间:2019-04-09 02:18:33

标签: excel vba

我是vba初学者,正在为一家小型公司构建CRM电子表格。我有一个包含公司/客户名称的工作表,我正在尝试从另一个工作表中提取他们的联系信息,并在弹出的用户窗体中显示它。

我的用户窗体使用文本框列出了各个联系人信息,因此我正在使用.Find / FindNext函数来填充它们。但是FindNext一直回卷到开头,导致用户窗体再次显示相同的名称。

如何在不使用循环的情况下停止.FindNext包装?

我尝试将其放入“循环”中,但这似乎使它陷入无限循环或其他状态并冻结了excel。我也尝试过没有运气的LastRow公式。

Sub UserForm_Activate()

Dim fSearch As Range 'the column we are searching in
Dim fFind As Range 'the value we are searching for
Dim LastRow As Long

Set fSearch = Sheets("Contact List").Range("Company_Find")

'First Find
Set fFind = fSearch.Find(What:=Selection.Value)
Debug.Print
    Txt_Contact1 = fFind.Offset(0, 5)
    Txt_Title1 = fFind.Offset(0, -1)
    Txt_Email1 = fFind.Offset(0, 1)
    Txt_Office1 = fFind.Offset(0, 2)
    Txt_Mobile1 = fFind.Offset(0, 3)

'Second Find
Set fFind = fSearch.FindNext(fFind)
Debug.Print
    Txt_Contact2 = fFind.Offset(0, 5)
    Txt_Title2 = fFind.Offset(0, -1)
    Txt_Email2 = fFind.Offset(0, 1)
    Txt_Office2 = fFind.Offset(0, 2)
    Txt_Mobile2 = fFind.Offset(0, 3)

'Third Find
Set fFind = fSearch.FindNext(fFind)
Debug.Print
    Txt_Contact3 = fFind.Offset(0, 5)
    Txt_Title3 = fFind.Offset(0, -1)
    Txt_Email3 = fFind.Offset(0, 1)
    Txt_Office3 = fFind.Offset(0, 2)
    Txt_Mobile3 = fFind.Offset(0, 3)

'Fourth Find

'Fifth Find

End Sub

1 个答案:

答案 0 :(得分:0)

    Set fFind = fSearch.Find(What:=Selection.Value)
    If Not fFind Is Nothing Then
        'Save the address of the first found range to compare with later
        Fadd = fFind.Address
    End If

    Do While Not fFind Is Nothing
         'Do stuff

         Set fFind = fSearch.FindNext(fFind)
         If Not fFind is Nothing Then
             'If the next found address is the same as the first, stop searching, exit the loop
             If fFind.Address = Fadd Then Exit Do
         End If
    Loop

这是我的方法。希望能帮助到你。我认为您没有正确退出Do...Loop,因此导致Excel死机的无限循环。 除非您更改第一个找到的范围的值,否则该循环将退出。

您最好使用循环,然后在每次搜索时编写Find方法。这样会导致硬编码的查找“循环”,而在迭代中没有灵活性。

编辑

下面的代码将循环填充UF中的所有文本框,并在所有文本框填充/未找到新值后退出循环

Dim ctrl as Control
Dim b as Integer

Set fFind = fSearch.Find(What:=Selection.Value)
If Not fFind Is Nothing Then
    b = 1
    'Save the address of the first found range to compare with later
    Fadd = fFind.Address
End If

Do While Not fFind Is Nothing
     For Each ctrl In Me.Controls
         If ctrl.Name Like "Txt_Contact" & b And ctrl.Value = "" Then ctrl.Value = fFind.Offset(0, 5)
         If ctrl.Name Like "Txt_Title" & b And ctrl.Value = "" Then ctrl.Value = fFind.Offset(0, -1)
         If ctrl.Name Like "Txt_Email" & b And ctrl.Value = "" Then ctrl.Value = fFind.Offset(0, 1)
         If ctrl.Name Like "Txt_Office" & b And ctrl.Value = "" Then ctrl.Value = fFind.Offset(0, 2)
         If ctrl.Name Like "Txt_Mobile" & b And ctrl.Value = "" Then ctrl.Value = fFind.Offset(0, 3)
     Next ctrl

     Set fFind = fSearch.FindNext(fFind)
     If Not fFind is Nothing Then
         'If the next found address is the same as the first, stop searching, exit the loop
         If fFind.Address = Fadd Then Exit Do
     End If
     b = b + 1
Loop