+ Reply to Thread
Results 1 to 4 of 4

Why is my nested countif loop wrong

Hybrid View

  1. #1
    Registered User
    Join Date
    04-22-2016
    Location
    Brussel, belgium
    MS-Off Ver
    2010
    Posts
    3

    Why is my nested countif loop wrong

    Hi all

    I have this code :

    Sub oldhome_homev2()
    
    Application.ScreenUpdating = False
    
    Dim i As Integer
    i = 0
     
    
    Do While Cells(21 + i, 31).Value <> ""
    
    i = i + 1
    If Application.WorksheetFunction.CountIf(Range("ai:ai"), (Cells(21 + i, 31)) > 0) Then
    
    Cells(21 + i, 37).Value = 55
    
    
    Else
    
    Cells(21 + i, 37).Value = Application.WorksheetFunction.CountIf(Range("ai:ai"), (Cells(21 + i, 31)))
       
       End If
          
     Loop
     
          
    
    End Sub
    what it should do :

    look if (Cells(21 + i, 31) exists in range ( ai:ai) ( i opted to do a countif , either its found so count is > 0 or not) -- if count > 0 then it should put value " 55 " in the cells if count < 0 then simply show the counted value

    somehow it always skips the first part of the if/ else condition and always applies the second part..



    dont understand why
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: Why is my nested countif loop wrong

    I haven't tested this but isn't the bracket in the wrong place?


    If Application.WorksheetFunction.CountIf(Range("ai:ai"), (Cells(21 + i, 31)) > 0) Then

    If Application.WorksheetFunction.CountIf(Range("ai:ai"), (Cells(21 + i, 31))) > 0 Then
    If a response has helped then please consider rating it by clicking on *Add Reputation below the post
    When your issue has been resolved don't forget to mark the thread SOLVED (click Thread Tools at top of thread)

  3. #3
    Registered User
    Join Date
    04-22-2016
    Location
    Brussel, belgium
    MS-Off Ver
    2010
    Posts
    3

    Re: Why is my nested countif loop wrong

    oh boy

    im not sure i understand why this works , but it did

    thanks a million... i will investigate to find out the logic

  4. #4
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: Why is my nested countif loop wrong

    The logic is

    IF

    the answer to this function Application.WorksheetFunction.CountIf(Range("ai:ai"), (Cells(21 + i, 31)))

    is greater than 0

    THEN

+ 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. Get wrong value in Nested If formula
    By acheek in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-14-2015, 05:05 PM
  2. [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
  3. [SOLVED] Find function in nested loop breaking down - not on first time through loop
    By adamstarr12 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-14-2014, 09:32 AM
  4. Find function in nested loop breaking down - not on first time through loop
    By adamstarr12 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2014, 04:59 PM
  5. Next without For error in nested loop - Escaping a Nested Loop?
    By BeneRich in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-10-2012, 06:38 PM
  6. For each loop nested; getting stuck on one cell in first iteration of nested loop
    By Excel_vba in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-22-2009, 11:54 AM
  7. help on a nested countif/nested if formula
    By vickiemc in forum Excel General
    Replies: 2
    Last Post: 08-13-2008, 08:29 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