VBA Code to check if cell value already exists in the range while looping

Anaya Zeeshan

New Member
Joined
Nov 4, 2021
Messages
33
Office Version
  1. 365
Platform
  1. Windows
I am trying to write a code to loop through a range and copy cell values if the cell is not blank and cell value doesn't already exists in the range.
I have written this code but its checking the cell above but I want to check check MyValue in entire range not just the cell above. if myValue is unique only then I want the value too be pasted in column T


Sub Testing()
Dim i As Long
Dim LastRow As Long
Dim MyValue As Variant
Const StartRow As Byte = 4

LastRow = Range("P" & Rows.Count).End(xlUp).Row

For i = StartRow To LastRow
MyValue = Range("P" & i).Value
If MyValue <> "" Then
If Range("p" & i + 1) <> MyValue Then
Range("T" & i).Value = MyValue
End If
End If
Next i

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
In that case try
VBA Code:
Sub AnayaZeeshan()
Dim x As Variant
   With Range("P4", Range("P" & Rows.Count).End(xlUp))
      x = Application.Unique(.Value)
   End With
   If IsArray(x) Then
      Range("T" & Rows.Count).End(xlUp).Offset(1).Resize(UBound(x)).Value = x
   End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,025
Messages
6,128,356
Members
449,444
Latest member
abitrandom82

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