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(Selection, Selection.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(0, 8) = 1
End If
Next x
End Sub
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.
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(Selection, Selection.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(0, 8) = 1 End If Next x End Sub
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 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
Bookmarks