시트에서 고유 ID를 검색하고 같은 행에있는 관련 데이터를 표시하는 userform을 만듭니다.VBA UserForm 여러 레코드 찾기, 표시 및 순환
나는 다른 StackOverflow의 질문의 도움을 사용했지만 나를 위해 정확하게 작동하지 않습니다
내가 찾고 있어요 고유 한 ID로 여러 데이터 세트가 있습니다. 아래 코드는 find를 클릭하면 발견 된 첫 번째 레코드를 보여주고 얼마나 많은 레코드가 시트에 있는지 사용자에게 알려주는 메시지 상자가 나타납니다. 확인을 클릭하면 사용자 정의 폼이 닫힙니다.
편집하려면 확인을 클릭 한 후 사용자가 FindNext 버튼을 클릭하면 userform에 원래 검색과 일치하는 다른 모든 레코드가 표시됩니다. (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'라고 말할 수 있습니다. 그렇지 않은 경우 계속하십시오. –