Advertisement
Brovashift

NewPopListview

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