I've got a macro that i run that uses data from 3 locations. The problem is 1 of the locations does not have any data for this month. How can i get it to skip trying to run the average with no data? It stops when it can't do averages for a month with no data, I put that in bold text in the vba code.
Here is the what the data looks like when collection stopped. For November there is zero data for 2nd location.
Here is the what the data looks like when collection stopped. For November there is zero data for 2nd location.
rrea co 202311.xlsm | |||||
---|---|---|---|---|---|
A | B | C | |||
6650 | 05-Oct-2023 00:00 | 0.49 | |||
6651 | 05-Oct-2023 01:00 | 0.47 | |||
6652 | 05-Oct-2023 02:00 | 0.44 | |||
6653 | 05-Oct-2023 03:00 | 0.47 | |||
6654 | 05-Oct-2023 04:00 | 0.46 | |||
6655 | 05-Oct-2023 05:00 | 0.47 | |||
6656 | 05-Oct-2023 06:00 | 0.49 | |||
6657 | 05-Oct-2023 07:00 | 0.5 | 0.47375 | ||
6658 | 05-Oct-2023 08:00 | 0.5 | |||
6659 | 05-Oct-2023 09:00 | 0.43 | |||
6660 | 05-Oct-2023 10:00 | 0.43 | |||
6661 | 05-Oct-2023 11:00 | 0.42 | |||
6662 | 05-Oct-2023 12:00 | 0.41 | |||
6663 | 05-Oct-2023 13:00 | 0.4 | |||
6664 | 05-Oct-2023 14:00 | 0.42 | |||
6665 | 05-Oct-2023 15:00 | 0.42 | 0.42875 | ||
6666 | 05-Oct-2023 16:00 | 0.41 | |||
6667 | 05-Oct-2023 17:00 | 0.43 | |||
6668 | 05-Oct-2023 18:00 | 0.42 | |||
6669 | 05-Oct-2023 19:00 | 0.44 | |||
6670 | 05-Oct-2023 20:00 | 0.45 | |||
6671 | 05-Oct-2023 21:00 | 0.46 | |||
6672 | 05-Oct-2023 22:00 | 0.44 | |||
6673 | 05-Oct-2023 23:00 | 0.435714 | |||
6674 | 06-Oct-2023 00:00 | 0.45 | |||
6675 | 06-Oct-2023 01:00 | 0.39 | |||
6676 | 06-Oct-2023 02:00 | 0.43 | |||
6677 | 06-Oct-2023 03:00 | 0.42 | |||
6678 | 06-Oct-2023 04:00 | 0.42 | |||
6679 | 06-Oct-2023 05:00 | 0.42 | |||
6680 | 06-Oct-2023 06:00 | 0.44 | |||
6681 | 06-Oct-2023 07:00 | 0.5 | 0.43375 | ||
6682 | 06-Oct-2023 08:00 | 0.39 | |||
6683 | 06-Oct-2023 09:00 | ||||
6684 | 06-Oct-2023 10:00 | ||||
6685 | 06-Oct-2023 11:00 | ||||
6686 | 06-Oct-2023 12:00 | ||||
6687 | 06-Oct-2023 13:00 | ||||
6688 | 06-Oct-2023 14:00 | ||||
6689 | 06-Oct-2023 15:00 | ||||
6690 | 06-Oct-2023 16:00 | ||||
6691 | 06-Oct-2023 17:00 | ||||
6692 | 06-Oct-2023 18:00 | ||||
6693 | 06-Oct-2023 19:00 | ||||
6694 | 06-Oct-2023 20:00 | ||||
6695 | 06-Oct-2023 21:00 | ||||
6696 | 06-Oct-2023 22:00 | ||||
6697 | 06-Oct-2023 23:00 | ||||
6698 | 07-Oct-2023 00:00 | ||||
6699 | 07-Oct-2023 01:00 | ||||
6700 | 07-Oct-2023 02:00 | ||||
6701 | 07-Oct-2023 03:00 | ||||
6702 | 07-Oct-2023 04:00 | ||||
6703 | 07-Oct-2023 05:00 | ||||
6704 | 07-Oct-2023 06:00 | ||||
6705 | 07-Oct-2023 07:00 | ||||
6706 | 07-Oct-2023 08:00 | ||||
6707 | 07-Oct-2023 09:00 | ||||
6708 | 07-Oct-2023 10:00 | ||||
6709 | 07-Oct-2023 11:00 | ||||
6710 | 07-Oct-2023 12:00 | ||||
hahn co with macro |
VBA Code:
Sub stats()
Dim i, iday, iseg, istrt, istop, icnt, n, ntot, kstrthr, ktotdays, jj As Integer
Dim kmo, kk, itotdays, itothrs, icntgood, isite As Integer
Dim imo(12) As Integer
Dim tabnam As String
' get year from user
kyr = InputBox("enter year (yyyy)")
'Cells(1, 13) = InputBox("enter year (yyyy)")
'kyr = Cells(1, 13)
' get month from user
'Cells(1, 11) = InputBox("enter month (1 to 12)")
'kmo = Cells(1, 11)
kmo = InputBox("enter month (01 to 12)")
imo(1) = 31: imo(2) = 28: imo(3) = 31: imo(4) = 30: imo(5) = 31: imo(6) = 30
imo(7) = 31: imo(8) = 31: imo(9) = 30: imo(10) = 31: imo(11) = 30: imo(12) = 31
If kyr Mod 4 = 0 Then imo(2) = 29
'Cells(20, 11) = imo(2)
mostr$ = "janfebmaraprmayjunjulaugsepoctnovdec"
' calc max hours in ytd
itotdays = 0
For kk = 1 To kmo
itotdays = itotdays + imo(kk)
Next kk
itothrs = itotdays * 24
'Cells(2, 11) = kmo
' put raw data into wallace, hahnville, ama worksheet tabs
'wallace in column H (8), hahnville in col E (5), ama in col B (2)
' date in col A (1)
'calculate start hour
ktotdays = 0
If kmo = 1 Then
kstrthr = 1
Else
For kk = 1 To kmo - 1
ktotdays = ktotdays + imo(kk)
Next kk
kstrthr = ktotdays * 24 + 1
End If
' copy from raw worksheet to wallace, ama, hahnville worksheets
jj = 4
For kk = kstrthr To itothrs
Worksheets("wall co with macro").Cells(kk + 1, 1) = Worksheets("raw").Cells(jj, 1)
Worksheets("wall co with macro").Cells(kk + 1, 2) = Worksheets("raw").Cells(jj, 8)
Worksheets("hahn co with macro").Cells(kk + 1, 1) = Worksheets("raw").Cells(jj, 1)
Worksheets("hahn co with macro").Cells(kk + 1, 2) = Worksheets("raw").Cells(jj, 5)
Worksheets("ama co with macro").Cells(kk + 1, 1) = Worksheets("raw").Cells(jj, 1)
Worksheets("ama co with macro").Cells(kk + 1, 2) = Worksheets("raw").Cells(jj, 2)
jj = jj + 1
Next kk
'-------------------------------------------------------
For isite = 1 To 3
If isite = 1 Then
tabnam = "wall co with macro"
ElseIf isite = 2 Then
tabnam = "hahn co with macro"
Else
tabnam = "ama co with macro"
End If
Sheets(tabnam).Select
Cells(1, 13) = kyr
Cells(1, 11) = kmo
asumtot = 0
ntot = 0
GoTo nxt
'-------------------------NOT USED---------------------------------
' calculate 3 hr averages
asumtot = 0: ntot = 0
For iday = 1 To itotdays
For iseg = 1 To 3
istrt = (iday - 1) * 24 + (iseg - 1) * 8 + 2
istop = istrt + 7
asum = 0
For i = istrt To istop
asum = asum + Cells(i, 2)
Next i
aver = asum / 8
arange$ = "B" & istrt & ":" & "B" & istop
Set arang = Range(arange$)
icnt = Application.WorksheetFunction.Count(arang)
If icnt > 2 Then
Cells(istop, 3) = aver
End If
Next iseg
Next iday
'----------------------------END OF NOT USED----------------------
nxt:
'GoTo fin
' calculate 8 hour averages
For iday = 1 To itotdays
For iseg = 1 To 3
istrt = (iday - 1) * 24 + (iseg - 1) * 8 + 2
istop = istrt + 7
asum = 0
n = 0
For i = istrt To istop
arange$ = "B" & i
Set arang = Range(arange$)
icnt = Application.WorksheetFunction.Count(arang)
If icnt = 1 Then
asum = asum + Cells(i, 2)
n = n + 1
asumtot = asumtot + Cells(i, 2)
ntot = ntot + 1
End If
Next i
If n > 0 Then aver = asum / n
If n > 5 Then
Cells(istop, 3) = aver
End If
Next iseg
Next iday
'Cells(10, 10) = "here"
'GoTo fin
' calculate annual and monthly maxs and averages
Cells(3, 7) = asumtot / ntot
istrt = 2: istop = itothrs + 1
'find max year
arange$ = "B2" & ":" & "B" & istop
Set arang = Range(arange$)
mxyr = Application.WorksheetFunction.Large(arang, 1)
Cells(4, 7) = mxyr
' find max 8 hr
arange$ = "C2:C" & istop
Set arang = Range(arange$)
mx8hr = Application.WorksheetFunction.Large(arang, 1)
Cells(5, 7) = mx8hr
'find annual recovery
arange$ = "B2:B" & istop
Set arang = Range(arange$)
icntgood = Application.WorksheetFunction.Count(arang)
cntpct = icntgood / (itothrs) * 100
Cells(6, 7) = cntpct
[B] 'compute month average
istrt = istop - imo(kmo) * 24 + 1
arange$ = "B" & istrt & ":B" & istop
Set arang = Range(arange$)
moavg = Application.WorksheetFunction.Average(arang)
Cells(7, 7) = moavg[/B]
' compute monthly 1 hr max
momax = Application.WorksheetFunction.Large(arang, 1)
Cells(8, 7) = momax
'compue monthly 2nd 1 hr max
mo2max = Application.WorksheetFunction.Large(arang, 2)
Cells(8, 8) = mo2max
'compute max 8 hr for month
arange$ = "C" & istrt & ":C" & istop
Set arang = Range(arange$)
mo8hrmax = Application.WorksheetFunction.Large(arang, 1)
Cells(9, 7) = mo8hrmax
'compute 2nd max 8 hr for month
mo8hr2max = Application.WorksheetFunction.Large(arang, 2)
Cells(9, 8) = mo8hr2max
'compute monthly recovery
arange$ = "B" & istrt & ":B" & istop
'Cells(7, 20) = arange$
Set arang = Range(arange$)
icntgoodmo = Application.WorksheetFunction.Count(arang)
cntpctmo = icntgoodmo / (imo(kmo) * 24) * 100
Cells(10, 7) = cntpctmo
'put month labels on workbook
molbl$ = Mid$(mostr$, (kmo - 1) * 3 + 1, 3)
labl$ = molbl$ & " average"
Cells(7, 6) = labl$
labl$ = molbl$ & " 1-max"
Cells(8, 6) = labl$
labl$ = molbl$ & " 8-max"
Cells(9, 6) = labl$
labl$ = molbl$ & " recovery"
Cells(10, 6) = labl$
'put in other labels
Cells(3, 6) = "yr avg"
Cells(4, 6) = "yr 1-max"
Cells(5, 6) = "yr 8-max"
Cells(6, 6) = "ann recovery"
Cells(1, 10) = "month"
Cells(1, 12) = "year"
Cells(1, 3) = "8-hr"
Next isite
' print to summary page
Worksheets("macro summary").Cells(1, 13) = "month"
Worksheets("macro summary").Cells(1, 14) = kmo
Worksheets("macro summary").Cells(1, 15) = "year"
Worksheets("macro summary").Cells(1, 16) = kyr
fin:
End Sub