検索結果をリストボックスに表示する:Excel VBA入門 |
スポンサードリンク | |
Sub test() UserForm1.show End Sub |
'検索を実行します。部分一致検索を行っています。 Private Sub CommandButton1_Click() Dim lastRow As Long Dim myData, myData2(), myno Dim i As Long, j As Long, cn As Long ' If TextBox1.Value = "" Or TextBox2.Value = "" Then End '検索するデータを配列 myData に格納しています。 With Worksheets("Sheet1") '--- 2018/9/27修正しました lastRow = .Cells(Rows.Count, 1).End(xlUp).Row myData = .Range(.Cells(1, 1), .Cells(lastRow, 7)).Value End With '配列 myData の中で検索で一致したデータを配列 myData2 に格納しています。 ReDim myData2(1 To lastRow, 1 To 3) For i = LBound(myData) To UBound(myData) If myData(i, 2) Like "*" & TextBox1.Value & "*" And myData(i, 7) Like "*" & TextBox2.Value & "*" Then cn = cn + 1 myData2(cn, 1) = myData(i, 1) myData2(cn, 2) = myData(i, 2) myData2(cn, 3) = myData(i, 7) End If Next i '検索で一致したデータをリストボックスに表示します。 With ListBox1 .ColumnCount = 3 .ColumnWidths = "30;70;70" .List = myData2 End With End Sub '---------------------------------------------- 'リストボックス内のデータをダブルクリックするとシートのデータを選択します。 Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean) With Worksheets("Sheet1") .Range(.Cells(ListBox1.List(ListBox1.ListIndex, 0) + 1, 1), .Cells(ListBox1.List(ListBox1.ListIndex, 0) + 1, 13)).Select End With End Sub '---------------------------------------------- 'ユーザーフォームの初期設定:リストの全データを表示しています。 Private Sub UserForm_Initialize() Dim lastRow As Long Dim myData, myData2() Dim i As Long, j As Long With Worksheets("Sheet1") '--- 2018/9/27修正しました lastRow = .Cells(Rows.Count, 1).End(xlUp).Row myData = .Range(.Cells(1, 1), .Cells(lastRow, 7)).Value End With ReDim myData2(1 To lastRow, 1 To 3) For i = LBound(myData) To UBound(myData) myData2(i, 1) = myData(i, 1) myData2(i, 2) = myData(i, 2) myData2(i, 3) = myData(i, 7) Next i With ListBox1 .ColumnCount = 3 .ColumnWidths = "30;70;70" .List = myData2 End With End Sub |
'検索を実行します。部分一致検索を行っています。 Private Sub CommandButton1_Click() Dim lastRow As Long Dim myData, myData2(), myno Dim i As Long, j As Long, cn As Long Dim myBook As Workbook Set myBook = Workbooks("kensaku.xlsm") '検索するデータを配列 myData に格納しています。 With myBook.Worksheets("meibo") '--- 2018/9/27修正しました lastRow = .Cells(Rows.Count, 1).End(xlUp).Row myData = .Range(.Cells(1, 1), .Cells(lastRow, 7)).Value End With '配列 myData の中で検索で一致したデータを配列 myData2 に格納しています。 ReDim myData2(1 To lastRow, 1 To 3) For i = LBound(myData) To UBound(myData) If myData(i, 2) Like "*" & TextBox1.Value & "*" And myData(i, 7) Like "*" & TextBox2.Value & "*" Then cn = cn + 1 myData2(cn, 1) = myData(i, 1) myData2(cn, 2) = myData(i, 2) myData2(cn, 3) = myData(i, 7) End If Next i '検索で一致したデータをリストボックスに表示します。 With ListBox1 .ColumnCount = 3 .ColumnWidths = "30;70;70" .List = myData2 End With End Sub '---------------------------------------------- 'リストボックス内のデータをダブルクリックするとシートのデータを選択します。 Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean) Dim myBook As Workbook Set myBook = Workbooks("kensaku.xlsm") With myBook.Worksheets("meibo") .Activate '別ファイルのシートをアクティブにします。 .Range(.Cells(ListBox1.List(ListBox1.ListIndex, 0) + 1, 1), .Cells(ListBox1.List(ListBox1.ListIndex, 0) + 1, 13)).Select End With End Sub '---------------------------------------------- 'ユーザーフォームの初期設定:リストの全データを表示しています。 Private Sub UserForm_Initialize() Dim lastRow As Long Dim myData, myData2() Dim i As Long, j As Long Dim myBook As Workbook Set myBook = Workbooks.Open("c:\users\yone4\Documents\old_mydoc\HP用データファイル\HP_2010\kensaku.xlsm") ’---あらかじめファイルを開いている場合は↓を使う。ファイルを開く場合は上の行↑を使う ' Set myBook = Workbooks("kensaku.xlsm") With myBook.Worksheets("meibo") '--- 2018/9/27修正しました lastRow = .Cells(Rows.Count, 1).End(xlUp).Row myData = .Range(.Cells(1, 1), .Cells(lastRow, 7)).Value End With ReDim myData2(1 To lastRow, 1 To 3) For i = LBound(myData) To UBound(myData) myData2(i, 1) = myData(i, 1) myData2(i, 2) = myData(i, 2) myData2(i, 3) = myData(i, 7) Next i With ListBox1 .ColumnCount = 3 .ColumnWidths = "30;70;70" .List = myData2 End With End Sub |
Private Sub CommandButton1_Click() Dim lastRow As Long Dim myData, myData2(), myno Dim i As Long, j As Long, cn As Long Dim key1 As String, key2 As String, key3 As String, key4 As String If TextBox1.Value = "" Then key1 = "*" Else key1 = "*" & TextBox1.Value & "*" If TextBox2.Value = "" Then key2 = "*" Else key2 = "*" & TextBox2.Value & "*" If TextBox3.Value = "" Then key3 = "*" Else key3 = "*" & TextBox3.Value & "*" Dim ListNo As Long ListNo = ComboBox1.ListIndex If ListNo < 0 Then key4 = "*" Else key4 = ComboBox1.List(ListNo) End If With Worksheets("Sheet1") '--- 2018/9/27修正しました lastRow = .Cells(Rows.Count, 1).End(xlUp).Row myData = .Range(.Cells(1, 1), .Cells(lastRow, 7)).Value End With ReDim myData2(1 To lastRow, 1 To 3) For i = LBound(myData) To UBound(myData) If myData(i, 2) Like key1 And myData(i, 7) Like key2 And myData(i, 5) Like key3 And myData(i, 6) Like key4 Then cn = cn + 1 myData2(cn, 1) = myData(i, 1) myData2(cn, 2) = myData(i, 2) myData2(cn, 3) = myData(i, 7) End If Next i With ListBox1 .ColumnCount = 3 .ColumnWidths = "30;70;70" .List = myData2 End With End Sub ’------ Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean) With Worksheets("Sheet1") .Range(.Cells(ListBox1.List(ListBox1.ListIndex, 0) + 1, 1), .Cells(ListBox1.List(ListBox1.ListIndex, 0) + 1, 13)).Select End With End Sub '-------- Private Sub UserForm_Initialize() Dim lastRow As Long Dim myData, myData2() Dim i As Long, j As Long With Worksheets("Sheet1") '--- 2018/9/27修正しました lastRow = .Cells(Rows.Count, 1).End(xlUp).Row myData = .Range(.Cells(1, 1), .Cells(lastRow, 7)).Value End With ReDim myData2(1 To lastRow, 1 To 3) For i = LBound(myData) To UBound(myData) myData2(i, 1) = myData(i, 1) myData2(i, 2) = myData(i, 2) myData2(i, 3) = myData(i, 7) Next i With ListBox1 .ColumnCount = 3 .ColumnWidths = "30;70;70" .List = myData2 End With With ComboBox1 .RowSource = "Sheet1!O2:O48" End With End Sub '--------- Private Sub CommandButton2_Click() UserForm_Initialize End Sub |
スポンサードリンク
PageViewCounter
Since2006/2/27