Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private targetSheetName As String
- Private Sub UserForm_Initialize()
- ' Import CSV data into a new sheet
- Dim newSheet As Worksheet
- Set newSheet = Workbooks("RaceCardAnalyser.xlsm").Worksheets.Add
- With newSheet.QueryTables.Add(Connection:= _
- "TEXT;C:\Users\Contango\Desktop\Racecard Analysis Project\Racecards\racecards.csv", Destination:=newSheet.Range("A1"))
- .TextFileCommaDelimiter = True 'Set delimiter to comma
- .TextFileParseType = xlDelimited
- .Refresh
- End With
- ' Store the name of the new sheet
- targetSheetName = newSheet.Name
- ' Populate TreeView with data from imported sheet
- Dim currNode As Node
- Dim prevRaceCourse As String
- Dim raceCourse As String
- Dim raceTime As String
- Dim currRow As Long
- ' Start on row 2 to skip header row
- currRow = 2
- ' Loop through all rows of data
- Do While Worksheets(targetSheetName).Cells(currRow, 3).Value <> ""
- ' Get current race course and race time
- raceCourse = Worksheets(targetSheetName).Cells(currRow, 3).Value
- ' Convert the fractional value to a time value
- raceTime = Format(Worksheets(targetSheetName).Cells(currRow, 2).Value, "hh:mm")
- ' Check if race course has changed from previous row
- If raceCourse <> prevRaceCourse Then
- ' Add new parent node for race course
- Set currNode = TreeView1.Nodes.Add(, , raceCourse, raceCourse)
- ' Set parent node's tag to race course
- currNode.Tag = raceCourse
- ' Store current race course as previous race course
- prevRaceCourse = raceCourse
- End If
- ' Add child node for race time
- Set currNode = TreeView1.Nodes.Add(raceCourse, tvwChild, , Format(raceTime, "hh:mm"))
- ' Set child node's tag to race time
- currNode.Tag = raceTime
- ' Move to next row
- currRow = currRow + 1
- Loop
- ' Sort child nodes of each parent node
- Dim parentNode As Node
- For Each parentNode In TreeView1.Nodes
- Dim childNodes As Nodes
- Set childNodes = parentNode.Child
- childNodes.Sort Key1:=tvwText, Order1:=tvwAscending
- Next parentNode
- End Sub
- Private Sub UserForm_Terminate()
- ' NOTES: Remove the sheet created when the form was initialized
- Application.DisplayAlerts = False 'Suppress alert message
- Workbooks("RaceCardAnalyser.xlsm").Sheets(targetSheetName).Delete
- Application.DisplayAlerts = True
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement