Advertisement
Brovashift

Untitled

May 6th, 2023 (edited)
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Private targetSheetName As String
  2.  
  3. Private Sub TreeView1_BeforeLabelEdit(Cancel As Integer)
  4.     ' Check if the node is a child node
  5.    If Node.Parent <> "" Then
  6.         ' Cancel the label edit
  7.        Cancel = True
  8.     End If
  9. End Sub
  10.  
  11. Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
  12.    
  13.     ' Declare variables
  14.    Dim raceCourse As String
  15.     Dim raceTime As String
  16.     Dim targetSheet As Worksheet
  17.     Dim targetRow As Long
  18.     Dim offTime As Date
  19.     Dim course As String
  20.     Dim raceName As String
  21.     Dim raceDate As Date
  22.     Dim prizeMoney As String
  23.     Dim formattedPrizeMoney As String
  24.     Dim regex As Object
  25.     Dim raceDist As String
  26.     Dim raceClass As String
  27.     Dim raceType As String
  28.     Dim going As String
  29.     Dim fieldSize As String
  30.    
  31.     ' Check if clicked node is a child node
  32.    If Node.Parent <> "" Then
  33.        
  34.         ' Get race course and race time from clicked nodes
  35.        raceCourse = Node.Parent.Text
  36.         raceTime = Node.Text
  37.        
  38.         ' Set target sheet
  39.        Set targetSheet = Worksheets(targetSheetName)
  40.        
  41.         ' Find match in column B
  42.        On Error Resume Next
  43.         targetRow = targetSheet.Evaluate("match(timevalue(""" & raceTime & """),b:b,0)")
  44.         On Error GoTo 0
  45.        
  46.         If targetRow <> 0 Then
  47.            
  48.             ' Get race data from target row
  49.            offTime = targetSheet.Cells(targetRow, 2).Value
  50.             course = targetSheet.Cells(targetRow, 3).Value
  51.             raceName = targetSheet.Cells(targetRow, 4).Value
  52.             raceDate = DateValue(targetSheet.Cells(targetRow, 1).Value)
  53.             prizeMoney = targetSheet.Cells(targetRow, 10).Value
  54.             raceDist = targetSheet.Cells(targetRow, 5).Value
  55.             raceClass = targetSheet.Cells(targetRow, 6).Value
  56.             raceType = targetSheet.Cells(targetRow, 7).Value
  57.             going = targetSheet.Cells(targetRow, 9).Value
  58.             fieldSize = targetSheet.Cells(targetRow, 8).Value
  59.            
  60.             ' Create a regular expression object
  61.            Set regex = CreateObject("VBScript.RegExp")
  62.             With regex
  63.                 .Pattern = "\D" ' Match any non-digit character
  64.                .Global = True ' Match all occurrences
  65.            End With
  66.            
  67.             ' Remove non-numeric characters from prizeMoney
  68.            prizeMoney = regex.Replace(prizeMoney, "")
  69.             formattedPrizeMoney = "£" & Format(Val(prizeMoney), "#,##0")
  70.            
  71.             ' Display race data in labels
  72.            TimeLbl.Caption = Format(offTime, "h:mm")
  73.             CourseLbl.Caption = course
  74.             RaceNameLbl.Caption = raceName
  75.             DateLbl.Caption = Format(raceDate, "dd mmm yy")
  76.             prizeLbl.Caption = formattedPrizeMoney
  77.             distLbl.Caption = raceDist & "f"
  78.             classLbl.Caption = raceClass
  79.             raceTypeLbl = raceType
  80.             goingLbl = going
  81.             runnersLbl = fieldSize
  82.            
  83.         Else
  84.             ' Display error message if no match was found
  85.            MsgBox "No race found for " & raceTime & " at " & raceCourse
  86.         End If
  87.     End If
  88.    
  89. End Sub
  90.  
  91. Private Sub UserForm_Initialize()
  92.     ' Import CSV data into a new sheet
  93.    Dim newSheet As Worksheet
  94.     Set newSheet = Workbooks("RaceCardAnalyser.xlsm").Worksheets.Add
  95.     With newSheet.QueryTables.Add(Connection:= _
  96.         "TEXT;C:\Users\Contango\Desktop\Racecard Analysis Project\Racecards\racecards.csv", Destination:=newSheet.Range("A1"))
  97.         .TextFileCommaDelimiter = True 'Set delimiter to comma
  98.        .TextFileParseType = xlDelimited
  99.         .Refresh
  100.     End With
  101.    
  102.     ' Store the name of the new sheet
  103.    targetSheetName = newSheet.Name
  104.    
  105.     ' Create dictionary to store racecourses and times
  106.    Dim raceData As Object
  107.     Set raceData = CreateObject("Scripting.Dictionary")
  108.    
  109.     ' Loop through all rows of data
  110.    Dim currRow As Long
  111.     For currRow = 2 To newSheet.Cells(newSheet.Rows.Count, "A").End(xlUp).Row
  112.         ' Get current race course and race time
  113.        Dim raceCourse As String
  114.         Dim raceTime As String
  115.         raceCourse = newSheet.Cells(currRow, 3).Value
  116.         raceTime = Format(newSheet.Cells(currRow, 2).Value, "h:mm")
  117.        
  118.         ' Add race course and race time to dictionary
  119.        If Not raceData.Exists(raceCourse) Then
  120.             raceData.Add raceCourse, New Collection
  121.         End If
  122.        
  123.         ' Add race time to collection if it doesn't already exist
  124.        Dim raceTimeCheck As Collection
  125.         Set raceTimeCheck = raceData(raceCourse)
  126.         Dim found As Boolean
  127.         found = False
  128.         Dim i As Long
  129.         For i = 1 To raceTimeCheck.Count
  130.             If raceTimeCheck(i) = raceTime Then
  131.                 found = True
  132.                 Exit For
  133.             ElseIf raceTimeCheck(i) > raceTime Then
  134.                 raceTimeCheck.Add raceTime, Before:=i
  135.                 found = True
  136.                 Exit For
  137.             End If
  138.         Next i
  139.         If Not found Then
  140.             raceTimeCheck.Add raceTime
  141.         End If
  142.     Next currRow
  143.    
  144.     ' Populate TreeView with data from dictionary
  145.    Dim raceCourses As Variant
  146.     For Each raceCourses In raceData
  147.         ' Add parent node for race course
  148.        Dim currNode As Node
  149.         Set currNode = TreeView1.Nodes.Add(, , raceCourses, raceCourses)
  150.         currNode.Tag = raceCourses
  151.        
  152.         ' Add child nodes for race times
  153.        Dim raceTimes As Variant
  154.         For Each raceTimes In raceData(raceCourses)
  155.             Set currNode = TreeView1.Nodes.Add(raceCourses, tvwChild, , raceTimes)
  156.             currNode.Tag = raceTimes
  157.         Next raceTimes
  158.     Next raceCourses
  159.    
  160.     ' Clean up
  161.    Set newSheet = Nothing
  162.     Set raceData = Nothing
  163.    
  164. End Sub
  165.  
  166. Private Sub UserForm_Terminate()
  167.  
  168.     ' NOTES: Remove the sheet created when the form was initialized
  169.    Application.DisplayAlerts = False 'Suppress alert message
  170.    Workbooks("RaceCardAnalyser.xlsm").Sheets(targetSheetName).Delete
  171.     Application.DisplayAlerts = True
  172.    
  173. End Sub
  174.  
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement