CountIf always counted 1 extra (VBA)

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
I have this code to count the number of letter "R" in any given row, see test file below, these two rows contain 2x "R"

1668457386889.png


but my code is spitting out 3 every time... why is that?

1668457442855.png


code below:

VBA Code:
  Dim ws1 As Worksheet
  Dim lrNew As Long, n As Long, n1 As Long, n2 As Long, sr As Long
  Set ws1 = ActiveSheet  

  lrNew = ws1.Range("K" & Rows.Count).End(xlUp).Row
  sr = 2
  If ws1.Cells(lrNew, "K").Value Like "*CS55*" Then
    n = WorksheetFunction.CountIf(ws1.Range("K" & sr & ":K" & lrNew), "*R*")
  End If
  MsgBox n
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Since we do not know what rows those are, or what might be in the rows above them or below them, there is no way to tell.

FWIW, when you post an image of your data instead of something that can be copied and pasted into a spreadsheet, it is difficult for others to experiment with it. Which means your chances of getting help drop significantly. Instead, use the free XL2BB tool (link below) to post your data in a way that makes it accessible to others.

 
Upvote 0
Since we do not know what rows those are, or what might be in the rows above them or below them, there is no way to tell.

FWIW, when you post an image of your data instead of something that can be copied and pasted into a spreadsheet, it is difficult for others to experiment with it. Which means your chances of getting help drop significantly. Instead, use the free XL2BB tool (link below) to post your data in a way that makes it accessible to others.


okay thanks Rlv, this macro has to run on many excel files, these "CS55" "R" can appear on many rows, and any rows.
I actually have the test file uploaded on dropbox, see below, thanks !

 
Upvote 0
okay thanks Rlv, this macro has to run on many excel files, these "CS55" "R" can appear on many rows, and any rows.
I actually have the test file uploaded on dropbox, see below, thanks !


Unfortunately, for security reasons on this particular PC, I cannot DL random files from the internet. Your best bet is to post a small data subset that replicates the issue. Or perhaps someone else will step in.

 
Upvote 0
Unfortunately, for security reasons on this particular PC, I cannot DL random files from the internet. Your best bet is to post a small data subset that replicates the issue. Or perhaps someone else will step in.

my Microsoft 365 blocks the xl2bb add-on every time I try to run it....
 
Upvote 0
The Drpbox file is different to the Screenshot in the first post ???
 
Upvote 0
2022100191 PAINT 2.csv
ABCDEFGHIJKLMNO
1Order#Str#QuantityItem#Job Req'd?Piece#Job#PriceSourceWeightDescriptionPART - Height (Int)PART - Catalog IDMH - SpecMH - Type
2CF222221011F14118JYesP1F22222DK010Production3973 lbMH,4'dia,Base,5"w18"18"FLORIDASANITARY
3CF222221011F14272NoP2F22222DK020Production5203 lbMH,4'dia,Riser,5"w72"72"FLORIDASANITARY
4CF222221011F14216NoP3F22222DK030Production1156 lbMH,4'dia,Riser,5"w16"16"FLORIDASANITARY
5CF222221011F14332GNoP4F22222DK040Production1677 lbMH,4'dia,Cone,5"w,SOG30"30"FLORIDASANITARY
6CF222221011No0Purchased0 lbEvergrip Sealant + 9" Wrapid-Seal0"170"/JOINTFLORIDASANITARY
7CF222221011No0Purchased0 lbEvergrip Sealant + 9" Wrapid-Seal0"170"/JOINTFLORIDASANITARY
8CF222221011No0Purchased0 lbEvergrip Sealant + 9" Wrapid-Seal0"170"/JOINTFLORIDASANITARY
9CF222221011No0Purchased-59 lbHOLE 12"0"FLORIDASANITARY
10CF222221011316.38 SqFt00 lbPaint,CS55,R/G/R0"FLORIDASANITARY
11CF222221011316.38 SqFt00 lbPaint,CS55,R/G/R0"FLORIDASANITARY
2022100191 PAINT 2


VBA Code:
    Dim ws1 As Worksheet
    Dim SA As Variant
    Dim I As Long
    Dim lrNew As Long, n As Long, sr As Long
    
    Set ws1 = ActiveSheet

    lrNew = ws1.Range("K" & Rows.Count).End(xlUp).Row
    sr = 2
    SA = ws1.Range("K" & sr & ":K" & lrNew).Value
    For I = LBound(SA) To UBound(SA)
        If InStr(SA(I, 1), "CS55") Then
            n = n + Len(SA(I, 1)) - (Len(Replace(SA(I, 1), "R", "", 1, , vbBinaryCompare)))
        End If
    Next I
    MsgBox "R's found: " & n
 
Upvote 0
2022100191 PAINT 2.csv
ABCDEFGHIJKLMNO
1Order#Str#QuantityItem#Job Req'd?Piece#Job#PriceSourceWeightDescriptionPART - Height (Int)PART - Catalog IDMH - SpecMH - Type
2CF222221011F14118JYesP1F22222DK010Production3973 lbMH,4'dia,Base,5"w18"18"FLORIDASANITARY
3CF222221011F14272NoP2F22222DK020Production5203 lbMH,4'dia,Riser,5"w72"72"FLORIDASANITARY
4CF222221011F14216NoP3F22222DK030Production1156 lbMH,4'dia,Riser,5"w16"16"FLORIDASANITARY
5CF222221011F14332GNoP4F22222DK040Production1677 lbMH,4'dia,Cone,5"w,SOG30"30"FLORIDASANITARY
6CF222221011No0Purchased0 lbEvergrip Sealant + 9" Wrapid-Seal0"170"/JOINTFLORIDASANITARY
7CF222221011No0Purchased0 lbEvergrip Sealant + 9" Wrapid-Seal0"170"/JOINTFLORIDASANITARY
8CF222221011No0Purchased0 lbEvergrip Sealant + 9" Wrapid-Seal0"170"/JOINTFLORIDASANITARY
9CF222221011No0Purchased-59 lbHOLE 12"0"FLORIDASANITARY
10CF222221011316.38 SqFt00 lbPaint,CS55,R/G/R0"FLORIDASANITARY
11CF222221011316.38 SqFt00 lbPaint,CS55,R/G/R0"FLORIDASANITARY
2022100191 PAINT 2


VBA Code:
    Dim ws1 As Worksheet
    Dim SA As Variant
    Dim I As Long
    Dim lrNew As Long, n As Long, sr As Long
   
    Set ws1 = ActiveSheet

    lrNew = ws1.Range("K" & Rows.Count).End(xlUp).Row
    sr = 2
    SA = ws1.Range("K" & sr & ":K" & lrNew).Value
    For I = LBound(SA) To UBound(SA)
        If InStr(SA(I, 1), "CS55") Then
            n = n + Len(SA(I, 1)) - (Len(Replace(SA(I, 1), "R", "", 1, , vbBinaryCompare)))
        End If
    Next I
    MsgBox "R's found: " & n
RLV, thank you !
care to explain what the vbBinaryCompare does in this code?

also, how do I modify the code so it only counts the number of "R" on any one row, not the total number of "R" between all relevant rows?
 
Upvote 0
care to explain what the vbBinaryCompare does in this code?
It makes the comparison case sensitive.


also, how do I modify the code so it only counts the number of "R" on any one row, not the total number of "R" between all relevant rows?
Your original VBA code example does not do that, it counts instances in all rows, so please illustrate what you mean by 'on any one row' and show what you do with the result. Use the data example I posted above to provides some example before and after values.
 
Upvote 0

Forum statistics

Threads
1,223,961
Messages
6,175,651
Members
452,664
Latest member
alpserbetli

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