+ Reply to Thread
Results 1 to 4 of 4

VBA to replace Countifs

Hybrid View

  1. #1
    Registered User
    Join Date
    05-15-2015
    Location
    England
    MS-Off Ver
    2010
    Posts
    2

    VBA to replace Countifs

    Hello,

    I have a spreadsheet that has 11 columns and almost 850,000 row. Each day should have 48 readings, but some have more, due to duplicates, others have less.
    When I use Countifs, it is very slow and constantly crashes. I was wondering if anyone had any suggestions on VBA code I could use?
    I have created a small sample sheet. It has less columns and rows, but should give you an idea of what I'm looking for.
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA to replace Countifs

    If this runs fine, replace Sheet.Add with Sheets("YourSheeName")
    Sub test()
        Dim a, e, i As Long, ii As Long
        Dim dic As Object, AL As Object
        Set dic = CreateObject("Scripting.Dictionary")
        Set AL = CreateObject("System.Collections.ArrayList")
        a = Sheets("eco").Cells(1).CurrentRegion.Value
        For i = 2 To UBound(a, 1)
            If Not dic.exists(a(i, 1)) Then
                Set dic(a(i, 1)) = CreateObject("Scripting.Dictionary")
            End If
            If Not AL.contains(a(i, 3)) Then AL.Add a(i, 3)
            dic(a(i, 1))(a(i, 3)) = dic(a(i, 1))(a(i, 3)) + 1
        Next
        ReDim a(1 To dic.Count + 1, 1 To AL.Count + 1): AL.Sort
        For ii = 0 To AL.Count - 1
            a(1, ii + 2) = AL(ii)
        Next
        i = 1
        For Each e In dic
            i = i + 1
            a(i, 1) = e
            For ii = 2 To UBound(a, 2)
                a(i, ii) = dic(e)(a(1, ii))
                If a(i, ii) = "" Then a(i, ii) = 0
            Next
        Next
        With Sheets.Add.Cells(1).Resize(UBound(a, 1), UBound(a, 2))
            .Value = a: .Cells(1).Value = "Site"
            Union(.Columns(1), .Rows(1)).Font.Bold = True
            .Sort .Cells(1), , , , , , , xlYes
            .HorizontalAlignment = xlCenter
            .Columns.AutoFit
        End With
    End Sub

  3. #3
    Registered User
    Join Date
    05-15-2015
    Location
    England
    MS-Off Ver
    2010
    Posts
    2

    Re: VBA to replace Countifs

    Thank you, this works perfectly

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA to replace Countifs

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

+ 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. [SOLVED] Sumproduct to replace countifs as countifs don't work on external source reference
    By KrishnaSagar in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 04-02-2017, 02:33 AM
  2. How can I run CountIfs + CountIfs without double counting?
    By hiitsjessie in forum Excel General
    Replies: 1
    Last Post: 02-06-2017, 04:49 PM
  3. COUNTIFS OR COUNTIFS needs to work
    By joshbellfi in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-23-2013, 10:55 AM
  4. [SOLVED] Using cell numbers to replace numerals in COUNTIFS criteria
    By tsig in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-30-2013, 02:03 AM
  5. COUNTIFS OR...multiple countifs without duplication in the numbers
    By HooligaD in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-21-2012, 09:53 AM
  6. Replies: 4
    Last Post: 02-06-2012, 12:16 PM
  7. Replies: 0
    Last Post: 12-16-2011, 09:01 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