I have a bit of code which looks through multiple sheets of data (each sheet represents 1 week) and then calculates the total across the sheets.
What I am now adding to this is the ability to chart the the values over a time period, on the click of a button. 1 button for each row in the table.
I can get this to work for each line in the table, but was wondering if there would be a quicker way other than copy and pasting the whole code, when the only change needed between rows is the value of Y.
Here is the code (this calculates for row 6, which means Y = 6)
What I am now adding to this is the ability to chart the the values over a time period, on the click of a button. 1 button for each row in the table.
I can get this to work for each line in the table, but was wondering if there would be a quicker way other than copy and pasting the whole code, when the only change needed between rows is the value of Y.
Here is the code (this calculates for row 6, which means Y = 6)
Code:
Sub Chart1()
Dim ws1 As Worksheet, _
ws2 As Worksheet, _
wsm As Worksheet, _
p As String, _
NumLoops As Long, _
WeekSNum As Long, _
X As Long, _
DT As Date, _
DF As Date, _
Y As Long, _
YearStart As Date
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
Set wsm = Sheets("Master")
ws1.Range("C5") = CDate(Sheets("Sheet1").txtDateFrom.Value)
ws1.Range("G5") = CDate(Sheets("Sheet1").txtDateTo.Value)
DF = (ws1.Range("C5") + 7) - (Weekday((ws1.Range("C5") + 7), 2))
DT = (ws1.Range("G5") + 7) - (Weekday((ws1.Range("G5") + 7), 2))
YearStart = ws1.Range("K30")
With wsm
.Cells.Clear
End With
NumLoops = (DT - DF) / 7 + 1
''Sets the value for NumLoops based on 2 date values in sheet1
WeekSNum = (DF - YearStart) / 7 + 1
''Sets the value for WeekSNum based on 2 values in sheet1
Y = 6
X = 0
''Sets the start value for X and Y
Do Until X = NumLoops
p = "Week" & WeekSNum + X
'MsgBox p
If Sheets(p).Range("W3") = "Total" Then
Sheets(p).Range("C" & Y, "U" & Y).Copy wsm.Range("C1").Offset(X, 0)
Sheets(p).Range("W" & Y).Copy wsm.Range("AE1").Offset(X, 0)
ElseIf Sheets(p).Range("Y3") = "Total" Then
Sheets(p).Range("C" & Y, "X" & Y).Copy wsm.Range("C1").Offset(X, 0)
Sheets(p).Range("Y" & Y).Copy wsm.Range("AE1").Offset(X, 0)
ElseIf Sheets(p).Range("AA3") = "Total" Then
Sheets(p).Range("C" & Y, "Z" & Y).Copy wsm.Range("C1").Offset(X, 0)
Sheets(p).Range("AA" & Y).Copy wsm.Range("AE1").Offset(X, 0)
ElseIf Sheets(p).Range("AC3") = "Total" Then
Sheets(p).Range("C" & Y, "AB" & Y).Copy wsm.Range("C1").Offset(X, 0)
Sheets(p).Range("AC" & Y).Copy wsm.Range("AE1").Offset(X, 0)
ElseIf Sheets(p).Range("AE3") = "Total" Then
Sheets(p).Range("C" & Y, "AD" & Y).Copy wsm.Range("C1").Offset(X, 0)
Sheets(p).Range("AE" & Y).Copy wsm.Range("AE1").Offset(X, 0)
ElseIf Sheets(p).Range("AG3") = "Total" Then
Sheets(p).Range("C" & Y, "AF" & Y).Copy wsm.Range("C1").Offset(X, 0)
Sheets(p).Range("AG" & Y).Copy wsm.Range("AE1").Offset(X, 0)
End If
X = X + 1
Loop
Sheets("Master").Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
r = 2
C = 2
With ActiveChart
.SetSourceData Source:=wsm.Range("C1:C" & X)
.SeriesCollection(1).Name = ws2.Range("C3")
Do Until r = 18
.SeriesCollection.NewSeries
.SeriesCollection(C).Values = wsm.Range("C1:C" & X).Offset(0, r)
.SeriesCollection(C).Name = ws2.Range("C3").Offset(0, r)
r = r + 2
C = C + 1
Loop
End With