How to use COUNTIF

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
I need to calculate the amount of CS55 in a bunch of excel files.
When column K contains CS55 and Black, we use this equation or if column K contains CS55 and two letter "B" use:
VBA Code:
n = n1 * 0.000666 * 7.48 * 2
when column K contains CS55 and one letter "B", use:
VBA Code:
n = n1 * 0.000666 * 7.48

full code below (add an additional row to display the amount of CS55), test file also attached
VBA Code:
Sub PaintCS55()
    Dim lrNew As Long, n1 As Long, n As Long, sr As Long
    lrNew = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    sr = 2
    If Cells(lrNew, "K").Value Like "*CS55**Black*" Then
         n1 = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*CS55**Black*")
         n = n1 * 0.000666 * 7.48 * 2
    End If
    If Cells(lrNew, "K").Value Like "*CS55*" And _
       WorksheetFunction.CountIf(Range(lrNew, "K"), "*B*") = 1 Then
         n1 = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*CS55**B*")
         n = n1 * 0.000666 * 7.48
    End If
    If Cells(lrNew, "K").Value Like "*CS55*" And _
       WorksheetFunction.CountIf(Range(lrNew, "K"), "*B*") = 2 Then
         n1 = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*CS55**B*")
         n = n1 * 0.000666 * 7.48 * 2
    End If
    If Cells(lrNew, "K").Value Like "*CS55*" And _
       WorksheetFunction.CountIf(Range(lrNew, "K"), "*B*") = 3 Then
         n1 = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*CS55**B*")
         n = n1 * 0.000666 * 7.48 * 3
    End If
    lrNew = lrNew + 1
    Cells(lrNew, "A") = Cells(lr, "A")
    Cells(lrNew, "B") = "."
    Cells(lrNew, "C") = n
    Cells(lrNew, "D") = "F50504"
    Cells(lrNew, "I") = "Purchased"
    Cells(lrNew, "K") = "CS55 Black"
    If Cells(lrNew, "C").Value Like "*0*" Then
        Rows(lrNew).Delete
    End If
End Sub

 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I need to calculate the amount of CS55 in a bunch of excel files.
When column K contains CS55 and Black, we use this equation or if column K contains CS55 and two letter "B" use:
VBA Code:
n = n1 * 0.000666 * 7.48 * 2
when column K contains CS55 and one letter "B", use:
VBA Code:
n = n1 * 0.000666 * 7.48

full code below (add an additional row to display the amount of CS55), test file also attached
VBA Code:
Sub PaintCS55()
    Dim lrNew As Long, n1 As Long, n As Long, sr As Long
    lrNew = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    sr = 2
    If Cells(lrNew, "K").Value Like "*CS55**Black*" Then
         n1 = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*CS55**Black*")
         n = n1 * 0.000666 * 7.48 * 2
    End If
    If Cells(lrNew, "K").Value Like "*CS55*" And _
       WorksheetFunction.CountIf(Range(lrNew, "K"), "*B*") = 1 Then
         n1 = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*CS55**B*")
         n = n1 * 0.000666 * 7.48
    End If
    If Cells(lrNew, "K").Value Like "*CS55*" And _
       WorksheetFunction.CountIf(Range(lrNew, "K"), "*B*") = 2 Then
         n1 = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*CS55**B*")
         n = n1 * 0.000666 * 7.48 * 2
    End If
    If Cells(lrNew, "K").Value Like "*CS55*" And _
       WorksheetFunction.CountIf(Range(lrNew, "K"), "*B*") = 3 Then
         n1 = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*CS55**B*")
         n = n1 * 0.000666 * 7.48 * 3
    End If
    lrNew = lrNew + 1
    Cells(lrNew, "A") = Cells(lr, "A")
    Cells(lrNew, "B") = "."
    Cells(lrNew, "C") = n
    Cells(lrNew, "D") = "F50504"
    Cells(lrNew, "I") = "Purchased"
    Cells(lrNew, "K") = "CS55 Black"
    If Cells(lrNew, "C").Value Like "*0*" Then
        Rows(lrNew).Delete
    End If
End Sub

it seems that I am using CountIf incorrectly, "Method of range of Global Object failed", the error is pointing at this part
VBA Code:
 If Cells(lrNew, "K").Value Like "*CS55*" And _
       WorksheetFunction.CountIf(Range(lrNew, "K"), "*B*") = 1 Then
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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