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
 
It makes the comparison case sensitive.



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.
Rlv, I've posted the rest of this macro below, but let me first explain what this entire macro does.

the macro will be run on many different excel files, these files may contain values like : Paint,CS55, R/G/R, Paint,CS55,R/B/G, Paint,CS55,B/R/R, Paint,CS55,R/R/R, Paint CS55, R/B, paint,CS55,Red...etc (the format of these values are very consistent).
Note: even know each file may contain any of the value above, but each file will only contain ONE of these values.
the CS55 is a paint material, the letter "R" is the first letter of the color red.
R/G/R is the order the paint being applied onto another product, in this case the product is painted red then gray then red paint.


I have a formula in the macro that calculates the gallonage of the red paint, based on the thickness of the paint.
So it is crucial to know how many layers of red paint is used on any given product.

Paint,CS55,R/G/R is two layers of red
Paint,CS55,R/R/R would be three layers
Paint,CS55,R/B would be one layer of the red paint
Paint,CS55,Red would be two layers

code below to calculate gallonage based on # of layers (this part of the code is working fine)
VBA Code:
  If ws1.Cells(lrNew, "K").Value Like "*CS55**Red*" Then
         n1 = WorksheetFunction.SumIfs(ws1.Range("C" & sr & ":C" & lrNew), ws1.Range("K" & sr & ":K" & lrNew), "*CS55**Red*")
         n2 = n1 * 0.000666 * 7.48 * 2
  End If

  If ws1.Cells(lrNew, "K").Value Like "*CS55*" And _
      n - 1 = 1 Then
         n1 = WorksheetFunction.SumIfs(ws1.Range("C" & sr & ":C" & lrNew), ws1.Range("K" & sr & ":K" & lrNew), "*CS55**R*")
         n2 = n1 * 0.000666 * 7.48
  End If

  If ws1.Cells(lrNew, "K").Value Like "*CS55*" And _
      n - 1 = 2 Then
         n1 = WorksheetFunction.SumIfs(ws1.Range("C" & sr & ":C" & lrNew), ws1.Range("K" & sr & ":K" & lrNew), "*CS55**R*")
         n2 = n1 * 0.000666 * 7.48 * 2
  End If

finally the macro add one additional row to the end of the excel file to display the gallonage and few other values. (this part is working fine)
VBA Code:
  lrNew = lrNew + 1
  ws1.Cells(lrNew, "A") = ws1.Cells(lrNew, "A")
  ws1.Cells(lrNew, "B") = "."
  ws1.Cells(lrNew, "C") = n2
  ws1.Cells(lrNew, "D") = "F50504"
  ws1.Cells(lrNew, "I") = "Purchased"
  ws1.Cells(lrNew, "K") = "CS55 Black"
  If ws1.Cells(lrNew, "C").Value Like "*0*" Then
    Rows(lrNew).Delete
  End If

full code
VBA Code:
Sub PaintCS55()
  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

  If ws1.Cells(lrNew, "K").Value Like "*CS55**Red*" Then
         n1 = WorksheetFunction.SumIfs(ws1.Range("C" & sr & ":C" & lrNew), ws1.Range("K" & sr & ":K" & lrNew), "*CS55**Red*")
         n2 = n1 * 0.000666 * 7.48 * 2
  End If

  If ws1.Cells(lrNew, "K").Value Like "*CS55*" And _
      n - 1 = 1 Then
         n1 = WorksheetFunction.SumIfs(ws1.Range("C" & sr & ":C" & lrNew), ws1.Range("K" & sr & ":K" & lrNew), "*CS55**R*")
         n2 = n1 * 0.000666 * 7.48
  End If

  If ws1.Cells(lrNew, "K").Value Like "*CS55*" And _
      n - 1 = 2 Then
         n1 = WorksheetFunction.SumIfs(ws1.Range("C" & sr & ":C" & lrNew), ws1.Range("K" & sr & ":K" & lrNew), "*CS55**R*")
         n2 = n1 * 0.000666 * 7.48 * 2
  End If

  lrNew = lrNew + 1
  ws1.Cells(lrNew, "A") = ws1.Cells(lrNew, "A")
  ws1.Cells(lrNew, "B") = "."
  ws1.Cells(lrNew, "C") = n2
  ws1.Cells(lrNew, "D") = "F50504"
  ws1.Cells(lrNew, "I") = "Purchased"
  ws1.Cells(lrNew, "K") = "CS55 Black"
  If ws1.Cells(lrNew, "C").Value Like "*0*" Then
    Rows(lrNew).Delete
  End If
End Sub

the result: for the example test file above, the value for n should be 2, and the value in cell C in the new row should be: 26.23, or round up to 27.
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
If you just want to count the R's in each relevant row, then store that number in a cell somewhere, then a UDF makes more sense.

VBA Code:
Function RCount(Desc As String) As Long
    Dim ws1 As Worksheet
    Dim SA As Variant
    Dim I As Long
    Dim lrNew As Long, n As Long, sr As Long

    If InStr(Desc, "CS55") Then
        n = Len(Desc) - (Len(Replace(Desc, "R", "", 1, , vbBinaryCompare)))
    End If
    RCount = n
End Function

Test.xlsm
ABCDEFGHIJKLMNOP
1Order#Str#QuantityItem#Job Req'd?Piece#Job#PriceSourceWeightDescriptionR countPART - Height (Int)PART - Catalog IDMH - SpecMH - Type
2CF222221011F14118JYesP1F22222DK010Production3973 lbMH,4'dia,Base,5"w018"18"FLORIDASANITARY
3CF222221011F14272NoP2F22222DK020Production5203 lbMH,4'dia,Riser,5"w072"72"FLORIDASANITARY
4CF222221011F14216NoP3F22222DK030Production1156 lbMH,4'dia,Riser,5"w016"16"FLORIDASANITARY
5CF222221011F14332GNoP4F22222DK040Production1677 lbMH,4'dia,Cone,5"w,SOG030"30"FLORIDASANITARY
6CF222221011No0Purchased0 lbEvergrip Sealant + 9" Wrapid-Seal00"170"/JOINTFLORIDASANITARY
7CF222221011No0Purchased0 lbEvergrip Sealant + 9" Wrapid-Seal00"170"/JOINTFLORIDASANITARY
8CF222221011No0Purchased0 lbEvergrip Sealant + 9" Wrapid-Seal00"170"/JOINTFLORIDASANITARY
9CF222221011No0Purchased-59 lbHOLE 12"00"FLORIDASANITARY
10CF222221011316.38 SqFt00 lbPaint,CS55,R/G/R20"FLORIDASANITARY
11CF222221011316.38 SqFt00 lbPair,CS55,R/G/R20"FLORIDASANITARY
Sheet18
Cell Formulas
RangeFormula
L2:L11L2=rcount(K2)
 
Upvote 0
If you just want to count the R's in each relevant row, then store that number in a cell somewhere, then a UDF makes more sense.

VBA Code:
Function RCount(Desc As String) As Long
    Dim ws1 As Worksheet
    Dim SA As Variant
    Dim I As Long
    Dim lrNew As Long, n As Long, sr As Long

    If InStr(Desc, "CS55") Then
        n = Len(Desc) - (Len(Replace(Desc, "R", "", 1, , vbBinaryCompare)))
    End If
    RCount = n
End Function

Test.xlsm
ABCDEFGHIJKLMNOP
1Order#Str#QuantityItem#Job Req'd?Piece#Job#PriceSourceWeightDescriptionR countPART - Height (Int)PART - Catalog IDMH - SpecMH - Type
2CF222221011F14118JYesP1F22222DK010Production3973 lbMH,4'dia,Base,5"w018"18"FLORIDASANITARY
3CF222221011F14272NoP2F22222DK020Production5203 lbMH,4'dia,Riser,5"w072"72"FLORIDASANITARY
4CF222221011F14216NoP3F22222DK030Production1156 lbMH,4'dia,Riser,5"w016"16"FLORIDASANITARY
5CF222221011F14332GNoP4F22222DK040Production1677 lbMH,4'dia,Cone,5"w,SOG030"30"FLORIDASANITARY
6CF222221011No0Purchased0 lbEvergrip Sealant + 9" Wrapid-Seal00"170"/JOINTFLORIDASANITARY
7CF222221011No0Purchased0 lbEvergrip Sealant + 9" Wrapid-Seal00"170"/JOINTFLORIDASANITARY
8CF222221011No0Purchased0 lbEvergrip Sealant + 9" Wrapid-Seal00"170"/JOINTFLORIDASANITARY
9CF222221011No0Purchased-59 lbHOLE 12"00"FLORIDASANITARY
10CF222221011316.38 SqFt00 lbPaint,CS55,R/G/R20"FLORIDASANITARY
11CF222221011316.38 SqFt00 lbPair,CS55,R/G/R20"FLORIDASANITARY
Sheet18
Cell Formulas
RangeFormula
L2:L11L2=rcount(K2)
thank you, I really like this new approach, I will play around with it for a bit and let you know how it went !
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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