Sum range composing of text and numbers

MikeL2023

New Member
Joined
Feb 6, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
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.

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?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Modified to code to find the last row in AQ....but some sample data would make it easier to possibly modify further.
VBA Code:
Dim lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For Each val In Sheets("Data Entry").Range("AQ22:AQ" & lr)
 
Upvote 1
This code finds the last used cell in AQ, loops through only the cells with text values between AQ22:AQ(lastrow) and converts them to numbers, then sums and averages the resulting values, less the count of any blank cells in the range.
VBA Code:
Sub MikeL()
Dim lastrow As Long, ce As Range, ws As Worksheet, rng As Range
Set ws = Sheets("Data Entry")
lastrow = ws.Range("AQ" & Rows.Count).End(xlUp).Row
Set rng = ws.Range("AQ22:AQ" & lastrow)

For Each ce In rng.SpecialCells(xlCellTypeConstants, 2)
    ce.Value = CDbl(Left(ce.Value, 5))
Next ce

Sheets("Calculations").Range("I14").Value = _
    Round(WorksheetFunction.Sum(rng) / (lastrow - 21 - rng.SpecialCells(xlCellTypeBlanks).Count), 2)
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top