Counting the number of cells with two digits

vixul

New Member
Joined
Sep 10, 2014
Messages
23
I cannot for the life of me figure out why I am getting a mismatch in the following code. I'm trying to have it just count the number of cells in a range with two digits. I highlighted the spot where I'm getting a mismatch error:


Code:
lastrow = foswork.Cells(foswork.Rows.Count, 2).End(xlUp).row
    For x = 1 To 5
        Set rng = foswork.Range(foswork.Cells(1, x + 5), foswork.Cells(lastrow, x + 5))
        staffing.Cells(x + 4, (3 * i) - 1) = _
            Application.WorksheetFunction.SumProduct(--[COLOR=#ff0000](Len(rng)[/COLOR] = 2)) - _
            Application.WorksheetFunction.CountIfs(rng, "SB")
    Next x

Thanks in advance for the help!
 
However, I think the issue with Marcelo's code is that rng.Address does not include the SHEET reference
and rng is built using the sheet foswork...


Try
Code:
lastrow = foswork.Cells(foswork.Rows.Count, 2).End(xlUp).Row

For x = 1 To 5
    Set rng = foswork.Range(foswork.Cells(1, x + 5), foswork.Cells(lastrow, x + 5))
    staffing.Cells(x + 4, (3 * i) - 1) = _
        Evaluate("=SUMPRODUCT(--(Len('" foswork.Name & "'!" & rng.Address & ") = 2))") - _
        Application.WorksheetFunction.CountIfs(rng, "SB")
Next x

This worked perfectly! Thanks so much!


I think the problem may be in the lack of a reference to the worksheet in my suggested replacement. Try this replacement instead...
Code:
Evaluate(Replace("SUM((@<>""SB"")*(LEN(@)=2))", "@", "'" & Rng.Parent.Name & "'!" & Rng.Address))

This still gave me the #VALUE in the cells, but the other code is doing what I want. Thanks for your help!
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,215,824
Messages
6,127,109
Members
449,359
Latest member
michael2

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