Complete SUB as requested.
Sorry notice i typed 22 instead of 2 for the row reference in my original post.
Public Sub DoGraph()
Dim MySQL As String
Dim MyCon As New Connection
Dim MyRec As New Recordset
Dim Count As Integer
Dim ufindate As Date
wks3.Activate
With wks3
.Cells(2, 1).Value = 9989
.Cells(3, 1).Value = 9990
.Cells(4, 1).Value = 9991
.Cells(5, 1).Value = 9992
.Cells(6, 1).Value = 9993
.Cells(7, 1).Value = 9994
.Cells(8, 1).Value = 9995
.Cells(9, 1).Value = 9996
.Cells(10, 1).Value = 9997
.Cells(11, 1).Value = 9998
.Cells(12, 1).Value = 9999
End With
If ConnectionString = "" Then
GetConnectionString
End If
'ClearData
'GetDates
MyCon.Open ConnectionString
MySQL = "select sum(uhours) uhours, uopnum, ufindate " & _
"from used, operators " & _
"where ufindate between " & TSStartDate & _
" and " & TSFinishDate & _
"and uoperator = oprid " & _
"and uopnum > 9000 " & _
"group by ufindate, uopnum " & _
"order by ufindate, uopnum"
MyRec.Open MySQL, MyCon
'Read each row of the record set
Count = 1
While Not MyRec.EOF
ufindate = MyRec!ufindate
uopnum = MyRec!uopnum
uhours = MyRec!uhours
If ufindate <> olddate Then
Count = Count + 1
End If
With wks3
.Cells(1, Count).Value = ufindate
.Cells((uopnum - 9989) + 2, Count).Value = uhours
End With
MyRec.MoveNext
olddate = ufindate
Wend
'Close efacs connections
MyRec.Close
MyCon.Close
MyRange = Range([A1], Cells(12, Count)).Address
Charts.Add
ActiveChart.ChartType = xlColumnStacked
ActiveChart.SetSourceData Source:=Sheets("Sheet2").Range(MyRange)
ActiveChart.Location Where:=xlLocationAsNewSheet ', Name:="Sheet2"
ActiveChart.PlotBy = xlRows
formular = "=sum(RC[-" & CStr(Count - 1) & "]:RC[-1])"
With wks3
.Cells(2, Count + 1).FormulaR1C1 = formular
.Cells(3, Count + 1).Formula = formular
.Cells(4, Count + 1).Formula = formular
.Cells(5, Count + 1).Formula = formular
.Cells(6, Count + 1).Formula = formular
.Cells(7, Count + 1).Formula = formular
.Cells(8, Count + 1).Formula = formular
.Cells(9, Count + 1).Formula = formular
.Cells(10, Count + 1).Formula = formular
.Cells(11, Count + 1).Formula = formular
.Cells(12, Count + 1).Formula = formular
End With
End Sub