Use a Strip and if else in spreadsheet to determine average.

Sayth

Board Regular
Joined
Jun 4, 2010
Messages
212
Hi

I want to complete this style of logic in Excel but am not sure how. \\
values in a table B2:M2, they will contain either 3 types of values M100, H100 or NA. After M there may be any different number result between 0 and 100. So (M80, M82, M64 are valid).

H stands for Home and M stands for Motor. In O2 I am trying to calculate the average of Home and P2 the average of Motor.

So In my head the logic is.

Code:
If cell value begins M then:
    strip the M:
       sumMotor =+ cell
          avg(sumMotor) into P2
    strip the H:
       sumHome =+ cell
          avg(sumHome) into P2
    else:
        next Cell
           if blank:
              break.
But I can never figure out VBA very well to know how to do this.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
For "M":
=AVERAGE(IF(LEFT(B2:M2)="M",SUBSTITUTE(B2:M2,"M",)*1)) Enter with Ctrl+ShiftEnter
For "H" change "M" to "H" in the formula

VBA code can be ...
Code:
Sub A()
[o2] = [AVERAGE(IF(LEFT(B2:M2)="H",SUBSTITUTE(B2:M2,"H",)*1))]
[p2] = [AVERAGE(IF(LEFT(B2:M2)="M",SUBSTITUTE(B2:M2,"M",)*1))]
End Sub
 
Last edited:
Upvote 0
For "M":
=AVERAGE(IF(LEFT(B2:M2)="M",SUBSTITUTE(B2:M2,"M",)*1)) Enter with Ctrl+ShiftEnter
For "H" change "M" to "H" in the formula

VBA code can be ...
Code:
Sub A()
[o2] = [AVERAGE(IF(LEFT(B2:M2)="H",SUBSTITUTE(B2:M2,"H",)*1))]
[p2] = [AVERAGE(IF(LEFT(B2:M2)="M",SUBSTITUTE(B2:M2,"M",)*1))]
End Sub
The previous post didn't handle the #N/A
this code will do ...
Code:
Sub A()
[o2] = [AVERAGE(IF(ISNA(B2:M2),"",IF(LEFT(B2:M2)="H",SUBSTITUTE(B2:M2,"H",)*1)))]
[p2] = [AVERAGE(IF(ISNA(B2:M2),"",IF(LEFT(B2:M2)="M",SUBSTITUTE(B2:M2,"M",)*1)))]
End Sub
 
Upvote 0
The previous post didn't handle the #N/A
this code will do ...
Code:
Sub A()
[o2] = [AVERAGE(IF(ISNA(B2:M2),"",IF(LEFT(B2:M2)="H",SUBSTITUTE(B2:M2,"H",)*1)))]
[p2] = [AVERAGE(IF(ISNA(B2:M2),"",IF(LEFT(B2:M2)="M",SUBSTITUTE(B2:M2,"M",)*1)))]
End Sub


It works for Home but if a person has No M type numbers then it supplies a Div/0 error.
 
Upvote 0
Change to ...
Code:
Sub A()
If [COUNTIF(B2:M2,"*H*")>0] Then [o2] = [AVERAGE(IF(ISNA(B2:M2),"",IF(LEFT(B2:M2)="H",SUBSTITUTE(B2:M2,"H",)*1)))]
If [COUNTIF(B2:M2,"*M*")>0] Then [p2] = [AVERAGE(IF(ISNA(B2:M2),"",IF(LEFT(B2:M2)="M",SUBSTITUTE(B2:M2,"M",)*1)))]
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,724
Messages
6,126,482
Members
449,316
Latest member
sravya

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