I am getting an error at the worksheetfunction.average line when I run this macro because of div/0 errors in the data. It runs fine when there are no div/0 errors in the code, so everything else is fine. I just need a way to be able to ignore the div/0 in the data, and preferably to filter out the zeros as well.
In this macro, I create a range to collect data from rngCollect, and then put every 4th value into an array. I then do some computations from that array, and that is where it hangs up when the data has errors in it. There is no way to get rid of the errors in the data. There are over 500 workbooks I am grabbing from, so I can't go through and manually filter.
wbData is defined in an earlier subroutine in this macro. Like I said there are no other issues with the code, it runs perfect when the data is full. Just need a way to filter the div/0 and zeros from the array so I can do the average, stdev, min, max calculations.
In this macro, I create a range to collect data from rngCollect, and then put every 4th value into an array. I then do some computations from that array, and that is where it hangs up when the data has errors in it. There is no way to get rid of the errors in the data. There are over 500 workbooks I am grabbing from, so I can't go through and manually filter.
wbData is defined in an earlier subroutine in this macro. Like I said there are no other issues with the code, it runs perfect when the data is full. Just need a way to filter the div/0 and zeros from the array so I can do the average, stdev, min, max calculations.
Code:
Public wbData As Workbook
Sub InputData()
Dim rngCollect As Range
Dim arrCOR As Variant, arrCT As Variant
Dim wsData As Worksheet, wsSamp As Worksheet
Dim i As Long, j As Long
Dim LastRow As Integer
Set wsData = wbData.Worksheets("Database")
Set wsSamp = wbData.Worksheets("Sample")
'Set range of data to collect from:
Set rngCollect = wsSamp.Range("C1", "C192")
ReDim arrCOR(1 To 48)
ReDim arrCT(1 To 48)
'Grab COR data which is every 4th row:
j = 1
For i = 3 To rngCollect.Count Step 4
arrCOR(j) = rngCollect(i)
j = j + 1
Next i
'Grab CT data which is every 4th row:
j = 1
For i = 4 To rngCollect.Count Step 4
arrCT(j) = rngCollect(i)
j = j + 1
Next i
'Input all the data into the database:
LastRow = wsData.UsedRange.Rows.Count + 1
wsData.Cells(LastRow, 1).Value = wsSamp.Range("M1").Value
wsData.Cells(LastRow, 2).Value = wsSamp.Range("M2").Value
wsData.Cells(LastRow, 3).Value = wsSamp.Range("M3").Value
wsData.Cells(LastRow, 4).Value = wsSamp.Range("M4").Value
wsData.Cells(LastRow, 5).Value = wsSamp.Range("G1").Value
wsData.Cells(LastRow, 6).Value = wsSamp.Range("G2").Value
wsData.Cells(LastRow, 7).Value = wsSamp.Range("G3").Value
wsData.Cells(LastRow, 8).Value = wsSamp.Range("G4").Value
wsData.Cells(LastRow, 9).Value = wsSamp.Range("G5").Value
wsData.Cells(LastRow, 10).Value = WorksheetFunction.Average(arrCT)
wsData.Cells(LastRow, 11).Value = WorksheetFunction.StDev(arrCT)
wsData.Cells(LastRow, 12).Value = WorksheetFunction.Max(arrCT)
wsData.Cells(LastRow, 13).Value = WorksheetFunction.Min(arrCT)
wsData.Cells(LastRow, 14).Value = wsData.Cells(LastRow, 12).Value - wsData.Cells(LastRow, 13).Value
wsData.Cells(LastRow, 15).Value = WorksheetFunction.Average(arrCOR)
wsData.Cells(LastRow, 16).Value = WorksheetFunction.StDev(arrCOR)
wsData.Cells(LastRow, 17).Value = WorksheetFunction.Max(arrCOR)
wsData.Cells(LastRow, 18).Value = WorksheetFunction.Min(arrCOR)
wsData.Cells(LastRow, 19).Value = wsData.Cells(LastRow, 17).Value - wsData.Cells(LastRow, 18).Value
wsData.Cells(LastRow, 20).Value = wsSamp.Range("J1").Value
wsData.Cells(LastRow, 21).Value = wsSamp.Range("J2").Value
wsData.Cells(LastRow, 22).Value = wsSamp.Range("J3").Value
wsData.Cells(LastRow, 23).Value = wsSamp.Range("J4").Value
wsData.Cells(LastRow, 24).Value = wsSamp.Range("J5").Value
End Sub
Last edited: