Do Loop with Countifs is not giving desired results in VBA

sanits591

Active Member
Joined
May 30, 2010
Messages
253
Hi

I have a code, which pulls data in few columns from a given data using RandBetween. To remove duplicates in column (except "X"), a code is given below:

This code is not giving the desired results, that is it stops itself before checking for any duplicates.

Kindly help in correction of this code.!

Code:
Sub VBA_Code_RandomwithRankModified()

Dim LastRow
Dim i As Integer
Dim j As Integer


LastRow = 17
LastCol = 9


Do
  For i = 2 To LastRow
  For j = 5 To LastCol
    
    Cells(i, j) = WorksheetFunction.Index(Range("K" & i & ":" & "N" & i), WorksheetFunction.Rank(Range("P" & i), Range("P" & i & ":" & "S" & i)))
      
[B]      If Application.WorksheetFunction.CountIfs(Range(Cells(2, j), Cells(i, j)), Cells(i, j), Range(Cells(2, j), Cells(i, j)), "<>" & "X") > 1 Then Exit For[/B]

 
    
  Next
  
  Next
    
Loop Until i > LastRow




End Sub
 
Last edited:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I don't think you need the do loop.

Did you check whether the If condition is met? Maybe it is met. So, the code exits the For loop.
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top