SUMIFS criteria 1st through 3rd digits =??

jkream

New Member
Joined
Jul 14, 2016
Messages
10
I am writing a macro to return the sum of the amounts in a column C (CHECK_AMT) when the criteria in column B are met. I am having trouble differentiating the column B (FILENO) results:

FILENOCHECK_AMT
100X2943175
100X3076200
100X69225
11BB4725
1B1083150
1B126625
1B99210
227X9617
24X10820
336X1301150
336X1683250
336X2037100
362X1275
369X154950
369X244250
425A2993100
425A3090200
425A320250
425B27225
4AA429450
4AA1868150
4AA236750
4BB1289500
4BB132650
4CC111510
4CC137650
4CC1456125
4CC912250
4DD13540
4DD161250
4DD27925
4DD53612.5
4DD54525
4DD99965
4Z20650
4Z7580
4Z7680
900G116120
900G125940
900G1830
90X3750

<tbody>
</tbody>

The first set of numbers in the file number is the client, the rest is useless. I need to sum by client. The number of rows will be variable, so I would like to select ALL of rows in each column without hard coding the range.
ie: I need to sum all the values if the file number begins with ONLY a 4, NOT 426 or 425, or 90, but NOT 900, 1,11, and 100 are all to be separate.
For this I am only summing the 4's and the 900's but show me once and I can expand on it if I need to.
I am writing this in Excel 2013, but one of my users is running 2003 and it has to be compatible.
This is my first attempt at VB Macros. Any help is appreciated.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Can you post your current code and explain how it isn't working as expected/wanted?
 
Upvote 0
This will execute faster if you can specify some sort of reasonable range rather than whole columns, but try this:

=SUMPRODUCT(--(LEFT(A:A)="4"),1-(ISNUMBER(MID(A:A,2,1)+0)),B:B)
 
Upvote 0
Can you post your current code and explain how it isn't working as expected/wanted?

This worked until I brought it over to Excel 2003, but it crashed on SortOn:=xlSortOnValues?

Sub Sort2()
'
' Sort2 Macro
'

'
Cells.Select

Dim sSheetName As String
Dim sDataRange As String

sSheetName = ActiveSheet.Name
sDataRange = Selection.Address

ActiveWorkbook.Worksheets(sSheetName).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(sSheetName).Sort.SortFields.Add Key:=Range _
("B:B"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets(sSheetName).Sort
.SetRange Range(sDataRange)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Cells.EntireColumn.AutoFit
End Sub
 
Upvote 0
This works GREAT in the Cell, how do I put this in a Macro? I get a Compile Error for:

Sub Test()
Range("K4").Select
Selection.NumberFormat=SUMPRODUCT(--(LEFT(A:A)="4"),1-(ISNUMBER(MID(A:A,2,1)+0)),B:B)

End Sub
 
Upvote 0
No need to Select, and it isn't a number format:

Code:
Range("K4").Formula = "=SUMPRODUCT(--(LEFT(A:A)=""4""),1-(ISNUMBER(MID(A:A,2,1)+0)),B:B)"
 
Upvote 0
Thank you Scott. It won't surprise you, but I'm very psyched that this works! And its compatible with 2003 as well?

You are right about selecting the whole column slowing it down. Is there a way to make it dynamic, so it selects only the cells in the columns that are being used?

Would you mind explaining to me what this line does exactly? It works for FILENO "4" but not for "900" I assume it is the number of digits you are looking at? But I am unsure of how this line is selecting the digits? This way I could adapt it for any number of digits I needed in the future.
 
Upvote 0
Thank you Scott. It won't surprise you, but I'm very psyched that this works! And its compatible with 2003 as well?

You are right about selecting the whole column slowing it down. Is there a way to make it dynamic, so it selects only the cells in the columns that are being used?

Would you mind explaining to me what this line does exactly? It works for FILENO "4" but not for "900" I assume it is the number of digits you are looking at? But I am unsure of how this line is selecting the digits? This way I could adapt it for any number of digits I needed in the future.
 
Upvote 0
Range("K4").Formula = "=SUMPRODUCT(--(LEFT(A:A)=""4""),1-(ISNUMBER(MID(A:A,2,1)+0)),B:B)"

If you wanted to check for 90, change ""4"" to ""90"", change the ISNUMBER(MID to start at the 3rd character, so ISNUMBER(MID(A:A,3,1)

Excel 2003 doesn't support whole column ranges for SUMPRODUCT, you will have to use a range, and yes there is a way.

Code:
Sub test()
Dim x As Long
x = Range("A" & Rows.Count).End(xlUp).Row
Range("K4").Formula = "=SUMPRODUCT(--(LEFT(A1:A" & x & ")=""4""),1-(ISNUMBER(MID(A1:A" & x & ",2,1)+0)),B1:B" & x & ")"
End Sub
 
Last edited:
Upvote 0
this new script works for 4
Does not work for 90 changing the 2 to a 3
Or for 900 changing the 2 to a 4?
 
Upvote 0

Forum statistics

Threads
1,214,996
Messages
6,122,636
Members
449,092
Latest member
bsb1122

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