Hi guys,
I have a bit of VBA that works, but seems wasteful, and I was hoping someone might be able to help me optimise it a bit, or suggest better ways to achieve the same result.
This is related to air tightness testing as part of the UK Building Regs (hence the multiple references to 'AT' and 'air').
Currently this runs through range 'AQ22:AQ1521" on the Data Entry sheet, however this is actually a dynamic range so it seems needless to run through the full range each time.
This range contains a pasted list of values exported from some proprietary software to a csv in a fixed format. The values can either be numeric or strings, with the strings always being a number (to 2 dp) followed by a space and the word 'assumed' in brackets. E.g. 15.00 (assumed)
The sub checks first if the cell is empty and if not, then checks if its numeric. If it is it adds the number to a running sum. If not, then it converts the number part of the string and adds that. It also counts each non-empty cell in the range.
Finally once the loop is done, it divides one by the other to get the average and writes it to cell 'I14' on the 'Calculations' sheet.
Can anyone take a look and suggest better ways of achieving this?
I have a bit of VBA that works, but seems wasteful, and I was hoping someone might be able to help me optimise it a bit, or suggest better ways to achieve the same result.
Sub CalcAT()
Dim val As Range
Dim airtotal As Double
Dim aircount As Double
Dim AvAT As Double
For Each val In Sheets("Data Entry").Range("AQ22:AQ1521").Cells
If IsEmpty(val.Value) = False Then
If Application.WorksheetFunction.IsNumber(val.Value) = True Then
airtotal = airtotal + val.Value
aircount = aircount + 1
Else
airtotal = airtotal + CDbl(Left(val.Value, 5))
aircount = aircount + 1
End If
End If
Next
AvAT = Round((airtotal / aircount), 2)
Sheets("Calculations").Range("I14").Value = AvAT
End Sub
This is related to air tightness testing as part of the UK Building Regs (hence the multiple references to 'AT' and 'air').
Currently this runs through range 'AQ22:AQ1521" on the Data Entry sheet, however this is actually a dynamic range so it seems needless to run through the full range each time.
This range contains a pasted list of values exported from some proprietary software to a csv in a fixed format. The values can either be numeric or strings, with the strings always being a number (to 2 dp) followed by a space and the word 'assumed' in brackets. E.g. 15.00 (assumed)
The sub checks first if the cell is empty and if not, then checks if its numeric. If it is it adds the number to a running sum. If not, then it converts the number part of the string and adds that. It also counts each non-empty cell in the range.
Finally once the loop is done, it divides one by the other to get the average and writes it to cell 'I14' on the 'Calculations' sheet.
Can anyone take a look and suggest better ways of achieving this?