+ Reply to Thread
Results 1 to 4 of 4

VBA: Help with countifs / Match

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-22-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    102

    VBA: Help with countifs / Match

    Hi,

    Summary: Need to find out Position (especially column ) of a certain value ( input will be through code or in cell) and pass it as a first parameter to Countifs function

    i have this ( headers)

    A1:City1 B1:City2 C1:City4 D1:City6 E1:City5.....and appropriate values under each header.

    ..and the user input is in A2:City4 ( example)

    i would need to find out the position of City ( primarily which column - here it would be column 3 or C) and then use this column as input to countifs to count the value.


    sub test
    value1=range("A2").value
    'HLUP is defined name for A1:E1
    Ymatch=Evaluate("=match(""" & value1 & """,HLUP,0)")
    y=evaluate("=countifs(column(Ymatch),"=10")
    msgbox y
    The parameter in Bold should be substitued suitably which should access the corresponding Column
    Last edited by vidyuthrajesh; 02-29-2012 at 02:01 PM. Reason: Fantastic !! Resolved...many thanks.

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: VBA: Help with countifs / Match

    Something along these lines, perhaps:

    Sub Test()
    
    Dim vFindValue As Variant
    Dim rngMatch As Range
    
    vFindValue=Range("A2").Value
    
    Set rngMatch=Range("HLUP").Find(vFindValue, Lookin:=xlValues, LookAt:=xlWhole, MatchCase:=False)
    
    If rngMatch Is Nothing Then
      MsgBox "Value not found"
    Else
      Msgbox WorksheetFunction.CountIf(rngMatch.EntireColumn,10)
    End If
    
    End Sub
    Edited to add: And you do know that you could do this with a single formula and not bother with macros at all, don't you?
    Last edited by Andrew-R; 02-29-2012 at 12:41 PM.

  3. #3
    Forum Contributor
    Join Date
    02-22-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    102

    Re: VBA: Help with countifs / Match

    Thanx..it is resolved ( to answer ur question : i have 15 such columns and hence i am using vba code). Also just curious to know what is the correct usage of Evaluate as the code below does not seem to work and gives me #NAME

    Evaluate("=countifs(rngmatch.entirecolumn,""" & vfindvalue1 & """ )") but the following works

    WorksheetFunction.CountIfs(rngMatch.EntireColumn, "" & vfindvalue1 & "")

    Note: I substitued the search criteria as Text instead of number... i tried ( in evaluate) 1/2/3 double quotes but could get only #NAME

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: VBA: Help with countifs / Match

    If you wanted to go with Evaluate then something like this...

    Evaluate("=countifs(" & rngmatch.entirecolumn.address & "," & chr(34) & vfindvalue1 & chr(34) & ")")
    Chr(34) is just equal to ASCII character 34, which is the " character, and rngmatch.entirecolumn.address will return the sheet address of the entire column, for example $A:$A.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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