0% found this document useful (0 votes)
22 views

VBA Excel

This VBA macro summarizes vehicle count and travel time data from multiple worksheets. It collects vehicle type, count, and mean travel time data and writes it to a summary sheet. It also creates a histogram chart on the summary sheet to visualize the travel time data for each direction in 2-minute intervals.

Uploaded by

dwaytikitan28
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

VBA Excel

This VBA macro summarizes vehicle count and travel time data from multiple worksheets. It collects vehicle type, count, and mean travel time data and writes it to a summary sheet. It also creates a histogram chart on the summary sheet to visualize the travel time data for each direction in 2-minute intervals.

Uploaded by

dwaytikitan28
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Sub SummarizeData()

Dim ws As Worksheet
Dim summarySheet As Worksheet
Set summarySheet = ThisWorkbook.Sheets("SUMMARY SHEET")
Dim rowCounter As Long
rowCounter = 2

' Clear existing data


summarySheet.Cells.Clear

' Set headers


summarySheet.Cells(1, 1).Value = "Turn Movement"
summarySheet.Cells(1, 2).Value = "Vehicle Type"
summarySheet.Cells(1, 3).Value = "Total Number"
summarySheet.Cells(1, 4).Value = "Mean Travel Time"

Dim vehicleCounts As New Dictionary


Dim vehicleTimes As New Dictionary
Dim timeDict As Dictionary ' Separate dictionary for time values

' Check if there are any existing chart objects and delete them
If summarySheet.ChartObjects.Count > 0 Then
summarySheet.ChartObjects.Delete
End If

For Each ws In ThisWorkbook.Sheets


If ws.Name <> "SUMMARY SHEET" Then
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

Dim directionName As String


Dim directionRow As Long
Dim directionChanged As Boolean

directionChanged = True ' Initialize to true to set directionName in


first iteration

' Initialize separate time dictionary for each sheet


Set timeDict = New Dictionary

For i = 2 To lastRow
If directionName <> ws.Name Then
directionName = ws.Name
directionRow = rowCounter
directionChanged = True
End If

Dim currentVehicleType As String


Dim totalNumber As Long
Dim totalTravelTime As Double
Dim meanTravelTime As Double

currentVehicleType = ws.Cells(i, "A").Value


If currentVehicleType <> "" And Not IsError(ws.Cells(i, "L").Value)
Then
If Not vehicleCounts.Exists(currentVehicleType) Then
vehicleCounts.Add currentVehicleType, 1
vehicleTimes.Add currentVehicleType, ws.Cells(i, "L").Value
Else
vehicleCounts(currentVehicleType) =
vehicleCounts(currentVehicleType) + 1
vehicleTimes(currentVehicleType) =
vehicleTimes(currentVehicleType) + ws.Cells(i, "L").Value
End If
End If

' Collect time data for the current sheet


If Not IsEmpty(ws.Cells(i, "I").Value) And Not IsError(ws.Cells(i,
"L").Value) Then
Dim timeValue As Date
timeValue = ws.Cells(i, "I").Value
' Round down to nearest 2-minute interval
timeValue = RoundDownToNearest2Minutes(timeValue)
If Not timeDict.Exists(timeValue) Then
timeDict.Add timeValue, 1
Else
timeDict(timeValue) = timeDict(timeValue) + 1
End If
End If

If directionChanged Then
summarySheet.Cells(directionRow, 1).Value = directionName
directionChanged = False ' Reset flag after setting
directionName
End If
Next i

' Write to summary sheet


Dim vehicleType As Variant
For Each vehicleType In vehicleCounts.Keys
summarySheet.Cells(rowCounter, 1).Value = directionName
summarySheet.Cells(rowCounter, 2).Value = vehicleType
summarySheet.Cells(rowCounter, 3).Value =
vehicleCounts(vehicleType)
summarySheet.Cells(rowCounter, 4).Value = vehicleTimes(vehicleType)
/ vehicleCounts(vehicleType)
rowCounter = rowCounter + 1
Next vehicleType

' Write total row


summarySheet.Cells(rowCounter, 1).Value = directionName
summarySheet.Cells(rowCounter, 3).Value = "Total"
summarySheet.Cells(rowCounter, 4).Value =
WorksheetFunction.SumIf(summarySheet.Range("A:A"), directionName,
summarySheet.Range("C:C"))
rowCounter = rowCounter + 1

' Create histogram


Dim chartRange As Range
Dim chtObj As ChartObject
Dim cht As chart

' Define chart range


Set chartRange = summarySheet.Range(summarySheet.Cells(2, 4),
summarySheet.Cells(rowCounter - 1, 4))

' Set chart data


Set chtObj = summarySheet.ChartObjects.Add(Left:=100, Width:=375,
Top:=75, Height:=225)
Set cht = chtObj.chart
With cht
.SetSourceData Source:=chartRange
.ChartType = xlColumnClustered
.HasTitle = True
.ChartTitle.Text = directionName & " - Travel Time Histogram"
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Text = "Time Entered"
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Text = "Total Number of Vehicles"
End With

' Populate chart with time data


Dim timeArray() As Variant
Dim countArray() As Variant
timeArray = timeDict.Keys
countArray = timeDict.Items
cht.SeriesCollection.NewSeries
cht.SeriesCollection(1).XValues = timeArray
cht.SeriesCollection(1).Values = countArray

' Format x-axis to display time in hh:mm format


With cht.Axes(xlCategory)
.CategoryType = xlCategoryScale
.TickLabelSpacing = 2
.TickLabels.NumberFormat = "hh:mm"
End With

End If
Next ws
End Sub

Function RoundDownToNearest2Minutes(timeValue As Date) As Date


Dim totalMinutes As Long
totalMinutes = Hour(timeValue) * 60 + Minute(timeValue)
totalMinutes = (totalMinutes \ 2) * 2
RoundDownToNearest2Minutes = TimeSerial(totalMinutes \ 60, totalMinutes Mod 60,
0)
End Function

You might also like