Advertisement
Brovashift

PopListview

May 18th, 2023
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Private Sub PopulateChildListView(horseName As String)
  2.    
  3.     ' Clear existing items from parent ListView
  4.    ListView2.ListItems.Clear
  5.    
  6.     ' Set the target sheet (Sheet2)
  7.    Dim targetSheet As Worksheet
  8.     Set targetSheet = Worksheets(dataSheetName) ' Replace "Sheet2" with the actual name of the sheet
  9.    
  10.     ' Find the last row in Sheet2
  11.    Dim lastRow As Long
  12.     lastRow = targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Row
  13.    
  14.     ' Flag variable to indicate if any matching data is found
  15.    Dim matchFound As Boolean
  16.     matchFound = False
  17.    
  18.     ' Loop through the rows in Sheet2 and populate the ChildListView with matching data
  19.    Dim i As Long
  20.     For i = 2 To lastRow ' Assuming the data starts from row 2
  21.        Dim horseNameSheet2 As String
  22.         horseNameSheet2 = targetSheet.Cells(i, 22).Value ' Assuming the horse name is in the first column of Sheet2
  23.        
  24.         ' Remove the region letters from the horse name in Sheet2
  25.        Dim regionPos As Integer
  26.         regionPos = InStr(horseNameSheet2, "(")
  27.         If regionPos > 0 Then
  28.             horseNameSheet2 = Trim(left(horseNameSheet2, regionPos - 1))
  29.         End If
  30.        
  31.         ' Compare the horse names
  32.        If StrComp(horseNameSheet2, horseName, vbTextCompare) = 0 Then
  33.             Dim listItem As MSComctlLib.listItem
  34.             Set listItem = ListView2.ListItems.Add(, , targetSheet.Cells(i, 1).Value) ' Assuming the data you want to display is in the second column of Sheet2
  35.            ' Add additional subitems if needed
  36.            
  37.             listItem.SubItems(1) = targetSheet.Cells(i, 22).Value ' Column 2
  38.            listItem.SubItems(2) = targetSheet.Cells(i, 3).Value ' Column 3
  39.            listItem.SubItems(3) = targetSheet.Cells(i, 5).Value ' Column 4
  40.            listItem.SubItems(4) = targetSheet.Cells(i, 13).Value ' Column 5
  41.            listItem.SubItems(5) = targetSheet.Cells(i, 15).Value ' Column 6
  42.            
  43.             Dim unformattedTime As String
  44.             unformattedTime = targetSheet.Cells(i, 27).Text ' Assuming the time value is stored as text in the cell
  45.            
  46.             ' Assign the unformatted time to the SubItems property
  47.            listItem.SubItems(6) = unformattedTime ' Column 7
  48.            
  49.             listItem.SubItems(7) = targetSheet.Cells(i, 18).Value ' Column 8
  50.            listItem.SubItems(8) = targetSheet.Cells(i, 16).Value ' Column 9
  51.            listItem.SubItems(9) = targetSheet.Cells(i, 21).Value ' Column 10
  52.            listItem.SubItems(10) = targetSheet.Cells(i, 7).Value ' Column 12
  53.            listItem.SubItems(11) = targetSheet.Cells(i, 25).Value ' Column 13
  54.            listItem.SubItems(12) = targetSheet.Cells(i, 33).Value ' Column 14
  55.            listItem.SubItems(13) = targetSheet.Cells(i, 34).Value ' Column 15
  56.            listItem.SubItems(14) = targetSheet.Cells(i, 26).Value ' Column 16
  57.            listItem.SubItems(15) = targetSheet.Cells(i, 19).Value ' Column 17
  58.            listItem.SubItems(16) = targetSheet.Cells(i, 17).Value ' Column 18
  59.            listItem.SubItems(17) = targetSheet.Cells(i, 30).Value ' Column 19
  60.            listItem.SubItems(18) = targetSheet.Cells(i, 32).Value ' Column 20
  61.            listItem.SubItems(19) = targetSheet.Cells(i, 39).Value ' Column 3
  62.            
  63.             matchFound = True ' Set the flag to indicate a match was found
  64.        End If
  65.     Next i
  66.    
  67.     ' Check if any matching data was found
  68.    If Not matchFound Then
  69.         MsgBox "No matching data found for horse name: " & horseName, vbInformation
  70.         ' Clear existing items from parent ListView
  71.        ListView2.ListItems.Clear
  72.     End If
  73.    
  74. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement