Advertisement
Brovashift

child node loop

Apr 29th, 2023 (edited)
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Private targetSheetName As String
  2. Private Sub UserForm_Initialize()
  3.     ' Import CSV data into a new sheet
  4.    Dim newSheet As Worksheet
  5.     Set newSheet = Workbooks("RaceCardAnalyser.xlsm").Worksheets.Add
  6.     With newSheet.QueryTables.Add(Connection:= _
  7.         "TEXT;C:\Users\Contango\Desktop\Racecard Analysis Project\Racecards\racecards.csv", Destination:=newSheet.Range("A1"))
  8.         .TextFileCommaDelimiter = True 'Set delimiter to comma
  9.        .TextFileParseType = xlDelimited
  10.         .Refresh
  11.     End With
  12.    
  13.     ' Store the name of the new sheet
  14.    targetSheetName = newSheet.Name
  15.    
  16.     ' Populate TreeView with data from imported sheet
  17.    Dim currNode As Node
  18.     Dim prevRaceCourse As String
  19.     Dim raceCourse As String
  20.     Dim raceTime As String
  21.     Dim currRow As Long
  22.    
  23.     ' Start on row 2 to skip header row
  24.    currRow = 2
  25.    
  26.     ' Loop through all rows of data
  27.    Do While Worksheets(targetSheetName).Cells(currRow, 3).Value <> ""
  28.         ' Get current race course and race time
  29.        raceCourse = Worksheets(targetSheetName).Cells(currRow, 3).Value
  30.        
  31.         ' Convert the fractional value to a time value
  32.        raceTime = Format(Worksheets(targetSheetName).Cells(currRow, 2).Value, "hh:mm")
  33.  
  34.        
  35.         ' Check if race course has changed from previous row
  36.        If raceCourse <> prevRaceCourse Then
  37.             ' Add new parent node for race course
  38.            Set currNode = TreeView1.Nodes.Add(, , raceCourse, raceCourse)
  39.            
  40.             ' Set parent node's tag to race course
  41.            currNode.Tag = raceCourse
  42.            
  43.             ' Store current race course as previous race course
  44.            prevRaceCourse = raceCourse
  45.         End If
  46.        
  47.         ' Add child node for race time
  48.        Set currNode = TreeView1.Nodes.Add(raceCourse, tvwChild, , Format(raceTime, "hh:mm"))
  49.        
  50.         ' Set child node's tag to race time
  51.        currNode.Tag = raceTime
  52.        
  53.         ' Move to next row
  54.        currRow = currRow + 1
  55.     Loop
  56.  
  57.     ' Sort child nodes of each parent node
  58.     Dim parentNode As Node
  59.     For Each parentNode In TreeView1.Nodes
  60.         Dim childNodes As Nodes
  61.         Set childNodes = parentNode.Child
  62.         childNodes.Sort Key1:=tvwText, Order1:=tvwAscending
  63.     Next parentNode
  64.  
  65.  
  66.  
  67.    
  68. End Sub
  69. Private Sub UserForm_Terminate()
  70.  
  71.     ' NOTES: Remove the sheet created when the form was initialized
  72.    Application.DisplayAlerts = False 'Suppress alert message
  73.    Workbooks("RaceCardAnalyser.xlsm").Sheets(targetSheetName).Delete
  74.     Application.DisplayAlerts = True
  75.    
  76. End Sub
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement