IREALLYambatman
Board Regular
- Joined
- Aug 31, 2016
- Messages
- 63
Hi guys, I have a macro where I am trying to create a chart where the X axis are dates, those values are currently in the format seen below seperated by a '|'.. I first take that cell of data, seperate it, then attempt to plot the data for the array. Does anyone know what I'm doing wrong?
The output I'm getting:
Some Example Date Formatting (far more data then this..):
Most likely problematic Parts Of code:
Whole Macro:
The output I'm getting:
Some Example Date Formatting (far more data then this..):
5/19/2023 8:42:00 AM|5/11/2023 9:27:00 AM|4/13/2023 1:04:00 PM |
Most likely problematic Parts Of code:
VBA Code:
valueArray = Split(valueCSV, "|")
dateArray = Split(dateCSV, "|")
If UBound(valueArray) <> UBound(dateArray) Then
MsgBox "Mismatch in the number of values and dates"
Exit Sub
End If
....more code...
Set dataSeries = .SeriesCollection.NewSeries
With dataSeries
.XValues = dateArray
.values = valuesArray
.Name = "Data Series"
End With
' Add high and low control limits as lines
Set dataSeries = .SeriesCollection.NewSeries
With dataSeries
.XValues = dateArray
.values = highLimitArray
.Name = "High Control Limit"
.ChartType = xlXYScatterLinesNoMarkers
End With
Set dataSeries = .SeriesCollection.NewSeries
With dataSeries
.XValues = dateArray
.values = lowLimitArray
.Name = "Low Control Limit"
.ChartType = xlXYScatterLinesNoMarkers
End With
.Axes(xlCategory, xlPrimary).CategoryType = xlTimeScale
.Axes(xlCategory, xlPrimary).TickLabels.NumberFormat = "mmm-dd-yyyy"
...more formatting stuff..
Whole Macro:
VBA Code:
Sub Chart()
Dim ws As Worksheet
Dim headerRange As Range, cell As Range
Dim selectedHeader As String
Dim valueCSV As String, dateCSV As String
Dim lowValue As Double, highValue As Double
Dim valueArray() As String, dateArray() As String
Dim highLimitArray() As Double, lowLimitArray() As Double
Dim i As Long
Dim chartObj As ChartObject
Dim dataSeries As Series
Dim rowNumber As Long, lastRow As Long
Set ws = ThisWorkbook.Sheets("Calculator")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).row
Set headerRange = ws.Range("A16:A" & lastRow)
selectedHeader = ws.Range("G6").Value
For Each cell In headerRange
If cell.Value = selectedHeader Then
rowNumber = cell.row
Exit For
End If
Next cell
If rowNumber = 0 Then
MsgBox "Header not found"
Exit Sub
End If
valueCSV = ws.Range("M" & rowNumber).Value
dateCSV = ws.Range("N" & rowNumber).Value
lowValue = ws.Range("E" & rowNumber).Value
highValue = ws.Range("F" & rowNumber).Value
valueArray = Split(valueCSV, "|")
dateArray = Split(dateCSV, "|")
If UBound(valueArray) <> UBound(dateArray) Then
MsgBox "Mismatch in the number of values and dates"
Exit Sub
End If
ReDim highLimitArray(1 To UBound(dateArray) + 1)
ReDim lowLimitArray(1 To UBound(dateArray) + 1)
For i = 1 To UBound(dateArray) + 1
highLimitArray(i) = highValue
lowLimitArray(i) = lowValue
Next i
Set chartObj = ws.ChartObjects.Add(Left:=100, Width:=375, Top:=50, Height:=225)
With chartObj.Chart
.ChartType = xlXYScatter
' Convert string array to double array for plotting
Dim valuesArray() As Double
ReDim valuesArray(1 To UBound(valueArray) + 1)
For i = LBound(valueArray) To UBound(valueArray)
valuesArray(i + 1) = CDbl(valueArray(i))
Next i
Set dataSeries = .SeriesCollection.NewSeries
With dataSeries
.XValues = dateArray
.values = valuesArray
.Name = "Data Series"
'.MarkerStyle = xlMarkerStyleCircle
'.MarkerSize = 5
'.MarkerBackgroundColor = RGB(0, 0, 255) ' Blue markers
'.MarkerForegroundColor = RGB(0, 0, 255) ' Blue markers
End With
' Add high and low control limits as lines
Set dataSeries = .SeriesCollection.NewSeries
With dataSeries
.XValues = dateArray
.values = highLimitArray
.Name = "High Control Limit"
.ChartType = xlXYScatterLinesNoMarkers
'.Format.Line.ForeColor.RGB = RGB(255, 0, 0) ' Red line for high limit
End With
Set dataSeries = .SeriesCollection.NewSeries
With dataSeries
.XValues = dateArray
.values = lowLimitArray
.Name = "Low Control Limit"
.ChartType = xlXYScatterLinesNoMarkers
'.Format.Line.ForeColor.RGB = RGB(0, 255, 0) ' Green line for low limit
End With
.Axes(xlCategory, xlPrimary).CategoryType = xlTimeScale
.Axes(xlCategory, xlPrimary).TickLabels.NumberFormat = "mmm-dd-yyyy"
.HasTitle = True
.ChartTitle.Text = selectedHeader
.ClearToMatchStyle
.ChartStyle = 268
End With
End Sub