+ Reply to Thread
Results 1 to 15 of 15

Need to properly dimension and define variable range for use in CountIf

  1. #1
    Registered User
    Join Date
    05-16-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    10

    Need to properly dimension and define variable range for use in CountIf

    Hi,

    I am having trouble setting up my variable range for use in a CountIf.

    First I select column A. Then go through each value in the column, looking for duplicates. I determine if it is a duplicate by checking all the cells above it for a match. The If statement should be false for the first value and true for all duplicate values, hence the "If countif > 1". I believe the problem is the variable range.

    Here is my code. Thanks for any help.

    PHP Code: 
    Sub T()

    Range("A2").Select
    Range
    (SelectionSelection.End(xlDown)).Select
    Dim x 
    As Range
    Dim y 
    As Long
    Dim Rng 
    As String


     
    For Each x In Selection
      y 
    x.Row
      Rng 
    = ("A2:A" y)
      If 
    Application.CountIf(Rng"x") > 1 Then
      x
    .Offset(08) = 1
      End 
    If
     
    Next x
    End Sub 

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Need to properly dimension and define variable range for use in CountIf

    try changing application.countif to worksheetfunction.countf
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    05-16-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Need to properly dimension and define variable range for use in CountIf

    Andy,

    I tried that and the error is "Type Mismatch" for the Rng in CountIf.

  4. #4
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    163

    Re: Need to properly dimension and define variable range for use in CountIf

    Because first argument in countif function must be Range data type and your is String

  5. #5
    Registered User
    Join Date
    05-16-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Need to properly dimension and define variable range for use in CountIf

    When I change the string to a range... The error message "Object variable or With block variable not set" pops up for Rng = ("A2:A" & y).

  6. #6
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    163

    Re: Need to properly dimension and define variable range for use in CountIf

    Because your Rng must be like this:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-16-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Need to properly dimension and define variable range for use in CountIf

    Thanks for the help so far, but although the code is free of errors, the IF statement is never true. For example when cells A2 and A3 have a value of 50, the statement is not true. In that case it should be true for A3.

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need to properly dimension and define variable range for use in CountIf

    AndyLitch in post #4 has the solution, to my eye. Be sure to make that edit. Show us your resulting code if it still isn't working.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  9. #9
    Registered User
    Join Date
    05-16-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Need to properly dimension and define variable range for use in CountIf

    This is my latest, but still not perfect code...

    When I step through it, I notice the If statement is never true.

    PHP Code: 
    Sub T()

    Range("A2").Select
    Range
    (SelectionSelection.End(xlDown)).Select
    Dim x 
    As Range
    Dim y 
    As Long
    Dim Rng 
    As Range


     
    For Each x In Selection
      y 
    x.Row
      Set Rng 
    Range("A2:A" y)
      If 
    WorksheetFunction.CountIf(Rng"x") > 0 Then
      x
    .Offset(08) = 1
      End 
    If
     
    Next x
    End Sub 

  10. #10
    Registered User
    Join Date
    05-16-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Need to properly dimension and define variable range for use in CountIf

    Oops. The > should be 1. I modified it to see if I could get it to be true.

    Here it is with > 1

    PHP Code: 
    Sub T()

    Range("A2").Select
    Range
    (SelectionSelection.End(xlDown)).Select
    Dim x 
    As Range
    Dim y 
    As Long
    Dim Rng 
    As Range


     
    For Each x In Selection
      y 
    x.Row
      Set Rng 
    Range("A2:A" y)
      If 
    WorksheetFunction.CountIf(Rng"x.value") > 1 Then
      x
    .Offset(08) = 1
      End 
    If
     
    Next x
    End Sub 

  11. #11
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    163

    Re: Need to properly dimension and define variable range for use in CountIf

    remove quot marks from "x.value"

  12. #12
    Registered User
    Join Date
    05-16-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Need to properly dimension and define variable range for use in CountIf

    YES! Yes yes yes!
    Thank you to Sbarro79, AndyLitch and JBeaucaire!

    I've tripled my VBA knowledge and productivity in just a few days because of helpful people like you.

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need to properly dimension and define variable range for use in CountIf

    Does this accomplish the same thing? It puts a regular formula into column I, all cells at once, then removes the formula leaving the results behind, no looping.
    Please Login or Register  to view this content.

  14. #14
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,523

    Re: Need to properly dimension and define variable range for use in CountIf

    Hi under00c,

    Here's a way to achieve your desired result without looping and hence much faster and efficient:

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  15. #15
    Registered User
    Join Date
    05-16-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Need to properly dimension and define variable range for use in CountIf

    Yes, brilliant. Ill use that one in my code. Im a beginner so looping was easier for me to understand.

    But now Ive learned a new method!

    Thanks again

+ 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