Counting if Values in range are numeric with vba

Nicha

New Member
Joined
Feb 10, 2023
Messages
48
Office Version
  1. 2016
Platform
  1. Windows
I need to count combinations (pairs of), caractère 'A' + numerical value with 3 digits. The caractère 'A' is in column (B) and the numerical value is in column (C). The important thing is that you can only count if the value in column (C) is numeric.

GroupBCResult
A1A1231
A1#0000
A1A5461
A1#9010
A2A6782
A2A6782
A2#5000
A3A5321
A3#0000
A3#0000
A4#10000
A4A10001


I tried to use the expression below, but it doesn't work because column (C) is formatted as (text); but it would work if it were formatted as (number).

VBA Code:
    'Rng_Value.cells(i).Value2 -> is the numéric value in column (C)
    vPairs = WorksheetFunction.CountIfs(Rng_Act, "A", Rng_Value, Rng_Value.cells(i).Value2, Rng_Value, ">0")

the code below works for value validation, but doesn't seem very robust since it only checks if we have a 'text' value.

VBA Code:
vPairs = WorksheetFunction.CountIfs(Rng_Act, "A", Rng_Value, Rng_Value.cells(i).Value2, Rng_Value, "*")


I am looking for a more precise way to validate that the value is numeric, with 3 characters. Something like:

VBA Code:
vPairs = WorksheetFunction.CountIfs(Rng_Act, "A", Rng_Value, Rng_Value.cells(i).Value2, Rng_Value, "isnumeric() and len() = 3")

Conclusion:
1) The most important thing is to count if 'A' + 'Value'. But if it is also possible to validate the length, even better.
2) In the table, for group 'A4' we have the pair 'A +1000' with the count result = 1 (green). Of course, if it was possible to validate the length too, since it is a value with 4 characters, the result should be = 0.

Can anyone help please?
My best regards.
 
Is there ever any entry with a length less than 3?
Or a numeric entry with a length more than three were the first number is a 0?

If the answer to both those is "No", then if you do convert them to numbers, you can just use "a number less than than or equal to 999" for that part of the criteria.
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Can you give an example with code, please?
It would just be a COUNTIFS formula, with the following criteria to include numbers between 0 and 999:
">0"
and
"<=999"
 
Upvote 0
Thamks.
I can't, for now, format as number. But if there's no other solution I'll have to change the algorithm.
 
Upvote 0
Thamks.
I can't, for now, format as number. But if there's no other solution I'll have to change the algorithm.
Or change your plan, and use VBA to loop through the records to check each one instead of trying to use Excel worksheet functions.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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