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.

[TABLE="width: 500"]
<tbody>[TR]
[TD][TABLE="width: 1027"]
<colgroup><col><col><col><col span="9"></colgroup><tbody>[TR]
[TD="colspan: 3"]Area 10[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]LIGHT[/TD]
[TD]MEDIUM[/TD]
[TD]HEAVY[/TD]
[TD]GRATING[/TD]
[TD]PLATE[/TD]
[/TR]
[TR]
[TD]L127x89x9.5[/TD]
[TD]71517[/TD]
[TD]1107[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]PL-10[/TD]
[TD]N/A[/TD]
[TD]104[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]PL-20[/TD]
[TD]N/A[/TD]
[TD]1120[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W150x22[/TD]
[TD]28777[/TD]
[TD]642[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W200x27[/TD]
[TD]38600[/TD]
[TD]1034[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W200x36[/TD]
[TD]8337[/TD]
[TD]297[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W200x42[/TD]
[TD]3100[/TD]
[TD]129[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W200x46[/TD]
[TD]12000[/TD]
[TD]554[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W250x49[/TD]
[TD]26801[/TD]
[TD]1316[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W310x39[/TD]
[TD]138000[/TD]
[TD]5340[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W310x45[/TD]
[TD]56500[/TD]
[TD]2522[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W310x52[/TD]
[TD]6000[/TD]
[TD]313[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W310x60[/TD]
[TD]110548[/TD]
[TD]6581[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W360x64[/TD]
[TD]26981[/TD]
[TD]1727[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Total kg[/TD]
[TD]22,785[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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