vba to add col C value if col A value contains

Curtisyoung78

New Member
Joined
Jun 19, 2017
Messages
25
Hi guys,

i am trying to write a vba to add values of col c (Weight) if the value in column a starts with a "W" and numbers after the "X" is less than 30 then add them all up and put the sum in cell H2(under Light). I've been working on this for a few days and cant quite figure it out, the syntax and "if then" i cant get quite right. can someone give me an example even, anything would help. thanks in advance.

Area 10LIGHTMEDIUMHEAVYGRATINGPLATE
L127x89x9.5715171107
PL-10N/A104
PL-20N/A1120
W150x2228777642
W200x27386001034
W200x368337297
W200x423100129
W200x4612000554
W250x49268011316
W310x391380005340
W310x45565002522
W310x526000313
W310x601105486581
W360x64269811727
Total kg22,785

<colgroup><col><col><col><col span="9"></colgroup><tbody>
</tbody>

<tbody>
</tbody>
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
No need for macros, you can do that with a formula


Excel 2013/2016
ABCDEFGHIJKL
1Area 10LIGHTMEDIUMHEAVYGRATINGPLATE
2L127x89x9.57151711071676
3PL-10N/A104
4PL-20N/A1120
5W150x2228777642
6W200x27386001034
7W200x368337297
8W200x423100129
9W200x4612000554
10W250x49268011316
11W310x391380005340
12W310x45565002522
13W310x526000313
14W310x601105486581
15W360x64269811727
asc
Cell Formulas
RangeFormula
H2=SUMPRODUCT(--(LEFT($A2:$A15,1)="W"),--(RIGHT($A2:$A15,2)*1<30),$C2:$C15)
 
Upvote 0
No need for macros, you can do that with a formula
That wouldn't work for values like
W200x100 (your formula would incorrectly include this row)
WPL-20 (your formula would incorrectly include this row)
W127x89x9.65 (your formula would incorrectly exclude this row)
 
Upvote 0
This array formula could be tried. It should be entered without the {} but confirmed with Ctrl+Shift+Enter, not just Enter. If confirmed correctly, Excel will insert the {}.
Code:
{=SUM(IF(LEFT($A$2:$A$15,1)="W",IF(ISNUMBER(FIND("x",$A$2:$A$15)),IF(ISNUMBER(RIGHT(SUBSTITUTE($A$2:$A$15,"x",REPT(" ",20)),20)+0),IF(RIGHT(SUBSTITUTE($A$2:$A$15,"x",REPT(" ",20)),20)+0<30,$C$2:$C$15)))))}
 
Upvote 0
That wouldn't work for values like
W200x100 (your formula would incorrectly include this row)
WPL-20 (your formula would incorrectly include this row)
W127x89x9.65 (your formula would incorrectly exclude this row)
Excellent points Peter.

A VBA option
Code:
Sub SumWeight()
   Dim cl As Range
   Dim Tot As Double
   
   For Each cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      If Left(cl, 1) = "W" And Right(cl, Len(cl) - InStrRev(cl, "x")) < 30 Then
         Tot = Tot + cl.Offset(, 2)
      End If
   Next cl
   Range("H2").Value = Tot
End Sub
 
Upvote 0
Excellent points Peter.

A VBA option
Code:
Sub SumWeight()
   Dim cl As Range
   Dim Tot As Double
   
   For Each cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      If Left(cl, 1) = "W" And Right(cl, Len(cl) - InStrRev(cl, "x")) < 30 Then
         Tot = Tot + cl.Offset(, 2)
      End If
   Next cl
   Range("H2").Value = Tot
End Sub


Worked perfectly, thanks very much. Not just a solution to a problem but also an excellent learning opportunity.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,936
Members
449,094
Latest member
teemeren

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