+ Reply to Thread
Results 1 to 19 of 19

WorksheetFunction.CountIf

  1. #1
    Registered User
    Join Date
    03-27-2019
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    9

    WorksheetFunction.CountIf

    Hi guys,

    I'm new to VBA but slowly learning, I'm not sure if what I am trying to do is possible in the way I'm trying to do it. Any help/guidance would be much appreciated.

    I am trying to set the value of a range of cells to be the count of a range of cells in another range. To try and keep it simple I am using this code:

    Range("J1:J5").Value = WorksheetFunction.CountIf(Range("A1:A20"), Range("I1:I5"))

    I get a mismatch error, as I said any help would be great.

    Thanks in advance.

  2. #2
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,767

    Re: WorksheetFunction.CountIf

    Formula like:
    Please Login or Register  to view this content.
    does not working as you think it should.

    If you want to use COUNTIF for multiple criteria you have to use:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    as array formula (means accept with Ctrl+Shift+Enter ---> {} should appear at start and end of formula).

    in macro:
    Please Login or Register  to view this content.
    Happy with my answer * Add Reputation.
    If You are happy with solution, please use Thread tools and mark thread as SOLVED.

  3. #3
    Registered User
    Join Date
    03-27-2019
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    9

    Re: WorksheetFunction.CountIf

    Thanks for the reply KOKOSEK, I probably should have been clearer with my question. I have a list of lets say 20 numbers in column A, and a list of 5 numbers in Column I, in Column J I want the macro to show how many times each number appears in column A. A separate value for each number, and I am hoping to do this with one line of code.

    Thanks again
    Attached Images Attached Images

  4. #4
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,767

    Re: WorksheetFunction.CountIf

    A bit more than 1 line:

    Please Login or Register  to view this content.
    or without VBA
    into J1:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    and drag it down

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Hi !

    Could be done with a single codeline if only a sample workbook was attached …

  6. #6
    Registered User
    Join Date
    03-27-2019
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    9

    Re: WorksheetFunction.CountIf

    Feel a little stupid but can't actually see how to attach, when I click on the Attachments button it just shows a small dropdown box with nothing in. Doesn't seem to be an option to actually attach anything.....I presume I'm missing something?

  7. #7
    Registered User
    Join Date
    03-27-2019
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    9

    Re: WorksheetFunction.CountIf

    As you can see:


    Capture.JPG

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Do not feel stupid as a new member with few posts you may not be yet authorized to attach any file …

    Wait a little until I have time to give it a try.

  9. #9
    Registered User
    Join Date
    03-27-2019
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    9

    Re: WorksheetFunction.CountIf

    Thanks Marc, to be honest I have simplified what I am trying to do just so it's easier to explain. What I actually want to do is have a list of product numbers in Column A (in the spreadsheet I'm testing with at the moment its approx 700 numbers), with numbers in row 2 from Columns E:AF. I want a CountIfs to count the amount of times product appears in a list on a separate tab where in the number in Row 2 also appears in another Column. Much easier to explain if I could attach obviously.

    Capture.JPG

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool


    Answer to post #3 : [J1:J5].Value2 = [IF({1},COUNTIF(A1:A20,I1:I5))]
    Last edited by Marc L; 06-21-2019 at 09:07 AM. Reason: $ not necessary

  11. #11
    Registered User
    Join Date
    03-27-2019
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    9

    Re: WorksheetFunction.CountIf

    Thanks Marc, just trying to get my head around why it works......the IF({1}......is stumping me?

  12. #12
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Without the IF the VBA Evaluate method - a must read within VBA inner help ! - returns wrong results
    (change your data with four 3 for example) …

    So the IF forces Evaluate method like "Hey, it's an array formula to apply here !" …

  13. #13
    Registered User
    Join Date
    03-27-2019
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    9

    Re: WorksheetFunction.CountIf

    Thanks very much Marc. Think it's now allowing me to attach things finally. So I am trying to adapt the solution you gave to fir my exact need, the workbook I've uploaded, in the Lines tab, I want the code to write the values in from B3:AC? - where ? is the amount of products in Column A.
    It needs to count the amount of times the product code appears in the Data with the corresponding RC in Column B from the Row 2 of the Lines tab....if that makes sense.
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question

    As I can't see any link with your initial post, clarify with a sample and its expected result …

    Is Inspection from Lines!B:C columns the same like R/C ?

  15. #15
    Registered User
    Join Date
    03-27-2019
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    9

    Re: WorksheetFunction.CountIf

    So if you were to think of it as a formula in Lines!B3 would be =COUNTIFS(Data!$A:$A,Lines!$A3,Data!$B:$B,Lines!B$2)
    Formula is to be expanded to all the rows with Products in and all the Columns up to AC

  16. #16
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Yes it's so obvious now !

    I have to change computer 'cause my actual version is too old (as COUNTIFS does not exist in 2003 version)
    and to test an alternative way which may be faster, so see you later …

  17. #17
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool The formula way …


    A demonstration as a beginner starter according to your formula & attachment :

    PHP Code: 
    Sub Demo1()
         
    Dim F$
        
    With Sheet3.UsedRange.Columns
             F 
    "=COUNTIFS(" & .Item(1).Address(External:=True) & ",$A3," & .Item(2).Address(External:=True) & ",B$2)"
        
    End With
        With Sheet5
    .UsedRange
            With 
    .Range("B3").Resize(.Rows.Count 2, .Columns.Count 1)
                 .
    Formula F
                 
    .Formula = .Value2
            End With
        End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  18. #18
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb An alternative way …


    So the faster way :

    PHP Code: 
    Sub Demo2()
                
    Dim VAVHV(), R&, C%, K$
        
    With Sheet3.UsedRange.Columns
            VA 
    = .Parent.Evaluate(.Item(1).Address "&""#""&" & .Item(2).Address)
        
    End With
        With CreateObject
    ("Scripting.Dictionary")
            For 
    Each VH In VA:  .Item(VH) = .Item(VH) + 1:  Next
        With Sheet5
    .UsedRange
            VA 
    = .Range("A3", .Cells(.Rows.Count1)).Value2
            VH 
    = .Range("B2", .Cells(2, .Columns.Count)).Value2
        End With
                ReDim V
    (1 To UBound(VA), 1 To UBound(VH2))
            For 
    1 To UBound(VA)
                For 
    1 To UBound(VH2)
                    
    VA(R1) & "#" VH(1C)
                    If .
    Exists(KThen V(RC) = .Item(K)
                
    Next
            Next
                
    .RemoveAll
        End With
            Sheet5
    .[B3].Resize(UBound(V), UBound(V2)).Value2 V
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  19. #19
    Registered User
    Join Date
    03-27-2019
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    9

    Re: WorksheetFunction.CountIf

    Thanks very much Marc.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Application.WorksheetFunction.CountIf in active row
    By Wijnand1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-13-2019, 07:57 AM
  2. [SOLVED] wrong result of worksheetFunction.countif
    By mohan.r1980 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-22-2018, 07:20 AM
  3. [SOLVED] Application.WorksheetFunction.CountIf not working right
    By Cbrehm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-20-2016, 04:49 PM
  4. [SOLVED] Optimizing WorksheetFunction.CountIf
    By daffodil11 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-25-2014, 05:06 PM
  5. Application.WorksheetFunction.CountIF
    By krsna83_pp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2011, 08:27 AM
  6. Error using WorksheetFunction.Countif
    By Kris_Wright_77 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-27-2006, 10:45 AM
  7. WorksheetFunction.CountIf
    By hotherps in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-28-2005, 11:31 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1