Results 1 to 3 of 3

Loop Column reference in VBA Countif

Threaded View

  1. #1
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Loop Column reference in VBA Countif

    Could somebody help me with this loop? I would like to compact this Countif to just a single line with the variable being passing thru as columns.

    I can fix the half to the left of the equal sign, but how can can I loop the column numbers? My attempts so far have failed so any help is appreciated.

    The columns to loop are 20, 4, 38, and 4, but of course there are more which I can easily put in once set on the right path.

    The 2nd criteria range in the Countif is always the same with the 2nd criteria already run through a For Each structure.

    Sub tryme()
        Dim x As Variant
        Dim i As Long: i = 3
        Dim wsSrc As Worksheet: Set wsSrc = Sheets("Projects")
        Dim wsDest As Worksheet: Set wsDest = Sheets("Stats")
        Dim LastRow As Long: LastRow = wsSrc.Range("A" & Rows.Count).End(xlUp).Row
        Dim wsf As Variant: Set wsf = Application.WorksheetFunction
        For Each x In Array("Gold", "Blue", "Orange", "Red")
            With wsSrc
                wsDest.Cells(2, i) = wsf.CountIfs(.Range(.Cells(2, 20), .Cells(LastRow, 20)), "", .Range(.Cells(2, 17), .Cells(LastRow, 17)), x)
                wsDest.Cells(3, i) = wsf.CountIfs(.Range(.Cells(2, 4), .Cells(LastRow, 4)), "", .Range(.Cells(2, 17), .Cells(LastRow, 17)), x)
                wsDest.Cells(4, i) = wsf.CountIfs(.Range(.Cells(2, 38), .Cells(LastRow, 38)), "", .Range(.Cells(2, 17), .Cells(LastRow, 17)), x)
                wsDest.Cells(5, i) = wsf.CountIfs(.Range(.Cells(2, 3), .Cells(LastRow, 3)), "", .Range(.Cells(2, 17), .Cells(LastRow, 17)), x)
                i = i + 1
            End With
        Next x
    End Sub
    Edit: I attached a sample workbook but limited the columns
    Attached Files Attached Files
    Last edited by jeffreybrown; 10-14-2016 at 08:00 PM.
    HTH
    Regards, Jeff

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] (Beginner help) For loop inside do loop that displays information from reference sheet.
    By lediable007 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-08-2016, 03:20 PM
  2. Why is my nested countif loop wrong
    By falco008 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-22-2016, 06:07 AM
  3. [SOLVED] For Loop nested in For Each Loop - Use For Each's [item] as row reference
    By Schluensen in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-28-2014, 11:37 PM
  4. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  5. [SOLVED] Loop till end of the sheet and loop back to first column again
    By smlim7 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-29-2012, 10:03 AM
  6. Loop a column on Sheet1 and loop a row on Sheet2 to find a match
    By johnnywinter in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-13-2009, 02:09 PM
  7. [SOLVED] For Each ... Next loop - need to reference the loop variable
    By neonx3@yahoo.ca in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-13-2006, 01:15 PM

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