Logical AND not working for checking Duplicates in VBA If Statement

gimmick18

New Member
Joined
Aug 14, 2019
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi Guys,

Been digging around looking for a way of capturing duplicates in rows of a Worksheet. I have two columns "E" and "B" where I need to jump out of the loop (LoopOut) if BOTH of them are the same. If I use the code below with a single instance of "Cells(rw."E") = Tag_Number.Value, then it captures duplicates in Column E without issue. If however I try to use the AND logical statement, it doesn't capture anything. It's quite happy to push through the entire For/Next loop and miss any duplicates.

The values in the referencing (Tag_Number.Value, and WON.Value) are coming from a Form that allows data entry, and this code runs under the "Submit" button. So the process is simple; enter all the details in on a Form then check if there are any duplicates. I just can't get the code below to check if duplicates exist across multiple columns.

What am I doing wrong in terms of synatax, for the IF statement?

Thanks in advance for any support the gurus can give.
Regards,
Kevin


' Duplicate Check
Dim W_Num As String
Dim W_Type As String
Dim T_Num As String
Dim rw As Integer

T_Num = Tag_Number
W_Num = WON
Application.ScreenUpdating = False
LastRow = Cells(Rows.Count, "E").End(xlUp).Row

For rw = 4 To LastRow
If (Cells(rw, "E") = Tag_Number.Value) And (Cells(rw, "B") = WON.Value) Then GoTo LoopOut
Next rw
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
What happens if you just check the values in col B?
Do you exit the loop?
 
Upvote 0
Hi Fluff!

Ah!....actually NO. If i modify to just check the "B" column....it fails to check it;

'Duplicate Check
Dim W_Num As String
Dim W_Type As String
Dim T_Num As String
Dim rw As Integer

T_Num = Tag_Number
W_Num = WON
Application.ScreenUpdating = False
LastRow = Cells(Rows.Count, "E").End(xlUp).Row

For rw = 4 To LastRow
If Cells(rw, "B") = WON.Value Then GoTo LoopOut
Next rw

This doesn't capture the duplicate. Interestingly; the data in the "B" column is just a number (eg. 40112248). Should I have defined W_Num as an Integer rather than a string because it is just grabbing numbers? Hadn't realised that it was the "B" that was failing to check for duplicated. Column "E" works fine.

Kevin
 
Upvote 0
Are the col B numbers whole numbers, or can you have decimals?
 
Upvote 0
Hi Fluff.......mystery solved, and I'm properly dumb!

I had set up the variable in DIM differently to how I was then setting their values further down (spelling; T_Type rather than W_Type in one instance). I was then using the Form values (WON_Type.Value) rather than the newly set up variable "W_Type".

So all in all, crap coding!

But your question over the WON caused me to focus in on that, and see the error in referencing.

Thanks v v much for the quick responses.
 
Upvote 0
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,239
Members
448,951
Latest member
jennlynn

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