2017-07-11 13 views
0

私は3つの基準に基づいてセルを見つけるために、以下のサブを持っています。VBA Userform ListBoxとTextBox

Private Sub FindEstimate_Click() 

Dim i As Long 

i = 5 
Do 
    If Cells(i, 1) = TextBox1 And Cells(i, 6) = ListBox1 And Cells(i, 9) = ListBox2 Then 
     Cells(i, 1).Select 
    End If 
    i = i + 1 
Loop Until Cells(i, 1) = TextBox1 And Cells(i, 6) = ListBox1 And Cells(i, 9) = ListBox2 

End Sub 

これは単純に機能せず、Lo​​op Until文と関係があると思われます。

+0

あなたは 'セル(I、6)= ListBox1'と'セル(I、9)= ListBox2'に確認し、正確に何をしようとしていますか? 'ListBox'の' Selected'項目は? –

+0

'Range.Find'はおそらくループよりも良いアイデアです。 –

+0

私は3つの基準に基づいて行の最初のセルを見つけようとしています。最初は見積もり番号です(例:「8888」「8889」)。もう1つは(列6)、サイト(Facebook *、Twitter *、Pinterest *など)です。列9は月(6/16、7/16-7/17)です。 –

答えて

1

あなたは、列「A」(場合にTextBox1.Valueと複数の一致がある)内のすべてのFind結果を通じてFind機能を使用してのより良い、とループ、あなたがListBox1.ValueListBox2.Valueでも一致を見つけることができるようになるまで。

このため、Do < - >Loop While Not FindRng Is Nothing And FindRng.Address <> FirstAddresループを使用します。

コード

Option Explicit 

Private Sub FindEstimate_Click() 

Dim Rng As Range 
Dim FindRng As Range 
Dim FirstRng As Range 
Dim FirstAddress As String 


Set Rng = Range("A5:A" & Cells(Rows.Count, "A").End(xlUp).Row) 

With Rng 
    Set FindRng = .Find(what:=Me.TextBox1.Value, LookIn:=xlValues, _ 
         lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext) 

    If Not FindRng Is Nothing Then ' find was successful 
     ' Set FirstRng = FindRng 
     FirstAddress = FindRng.Address 

     Do 
      If FindRng.Offset(, 5).Value = Me.ListBox1.Value And FindRng.Offset(, 8).Value = Me.ListBox2.Value Then 
       FindRng.Select ' <-- not sure why you need to select it 
       Exit Do 
      End If 
      Set FindRng = .FindNext(FindRng) 
     Loop While Not FindRng Is Nothing And FindRng.Address <> FirstAddress 

    Else ' Find faild to find the value in TextBox1 
     MsgBox "Unable to find " & Me.TextBox1.Value & " at column A" 
    End If 
End With 

End Sub 
関連する問題