私はシート上の一意のIDを検索し、同じ行にある関連データを表示するuserformを作成しています。VBA UserForm複数のレコードを検索し、表示して循環する
私は別のStackOverflowの質問の助けを使用しましたが、私が探してるのユニークなIDに複数のデータセットがありますが、私にとってまさに
を働いていません。下のコードはfindをクリックすると最初に見つかったレコードを表示し、シートにいくつのレコードがあるかをユーザーに知らせるメッセージボックスが表示されます。 [OK]をクリックすると、ユーザーフォームが閉じます。
[OK]をクリックした後、ユーザーが[FindNext]ボタンをクリックすると、元の検索と一致する他のすべてのレコードが表示されます。 (updateFields
でanchorCell.Select
参照)FindNext_Click
のコードは、最後の行が現在の選択となるように設定したことを示しているという事実を利用
Private Sub FindNext_Click()
Dim nextCell As Range
Set nextCell = Cells.FindNext(After:=ActiveCell)
'FindNext loops round to the initial cell if it finds no other so we test for it
If Not nextCell.Address(external:=True) = ActiveCell.Address(external:=True) Then
updateFields anchorCell:=nextCell
End If
End Sub
Private Sub Find_Click()
Worksheets("Master").Activate
Dim strFind As String
Dim FirstAddress As String
Dim rSearch As Range
Set rSearch = Range("a1", Range("a65536").End(xlUp))
Dim f As Integer
Dim c As Object
strFind = Me.TextBox1.Value
With rSearch
Set c = .Find(strFind, LookIn:=xlValues)
If Not c Is Nothing Then
updateFields anchorCell:=c
FirstAddress = c.Address
Do
f = f + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
If f > 1 Then
Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
Case vbCancel
End Select
Me.Height = frmMax
End If
Else: MsgBox strFind & " not listed"
End If
End With
End Sub
Private Sub updateFields(anchorCell As Range)
anchorCell.Select
With Me
.TextBox2.Value = anchorCell.Offset(0, 2).Value
.TextBox3.Value = anchorCell.Offset(0, 3).Value
.TextBox4.Value = anchorCell.Offset(0, 4).Value
.TextBox6.Value = anchorCell.Offset(0, 13).Value
.TextBox7.Value = anchorCell.Offset(0, 14).Value
.TextBox8.Value = anchorCell.Offset(0, 15).Value
.TextBox9.Value = anchorCell.Offset(0, 16).Value
.TextBox10.Value = anchorCell.Offset(0, 17).Value
.TextBox11.Value = anchorCell.Offset(0, 18).Value
.TextBox12.Value = anchorCell.Offset(0, 19).Value
.TextBox13.Value = anchorCell.Offset(0, 20).Value
.TextBox14.Value = anchorCell.Offset(0, 21).Value
.TextBox20.Value = anchorCell.Offset(0, 22).Value
End With
End Sub
おかげ
をnextingしている別のと別のアプローチを提案しますあなたがここで助けてください。あなたが 'OK'または' Cancel'が選択されているとき何もしていないので、主な問題はあなたの 'Select Case'ステートメントであると仮定できます。あなたのデータセットを検索するために 'Forループ 'に' With'ステートメントを囲むことができます。 'Case vbCancel:Exit For'と言うと、そうでなければ続行します。 –