VBA Comparing Strings to Check for Duplicates

Sunjinsak

Board Regular
Joined
Jul 13, 2011
Messages
143
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
Hi,</SPAN>

Using Excel 2003 SP3 on Windows XP.</SPAN>

I have a user form with 4 text boxes. The text box values can either be blank or contain 7 numeric characters.</SPAN>

I can control what is input into the text boxes using a combination of the MaxSize property and some basic code.</SPAN>

What I need though, once numeric entries have been made, is a way to take the value of each text box (preferably as a string as the values will eventually make their way into a Word template) and compare them to check if any of the values are the same.</SPAN>

None of the values can be identical except if they’re blank.</SPAN>

The only exception is if all the values are blank as there needs to be an entry in at least one box.</SPAN>

Does that make sense?</SPAN>

Can anyone help?</SPAN>

Thanks.</SPAN>
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Okay I’ve decided to give this one last bump as I really need an answer to this if possible and endless searching of goolge has so far been fruitless. Hopefully somebody here will be able to help.</SPAN></SPAN>

Mods – please forgive me. I won’t bump it again.</SPAN></SPAN>

See below for an example of the code I have so far and the bits where I’m missing the necessary code. If anyone can fill in the gaps it would be very much appreciated.</SPAN></SPAN>

There’s probably a much more efficient way to do what I’m trying to do but you get the gist of it.</SPAN></SPAN>

Code:
Sub Check_TextBoxes()
'Check there is at least one entry in at least one box
If UserForm1.TextBox0.Value = "" And UserForm1.TextBox1.Value = "" And _
   UserForm1.TextBox2.Value = "" And UserForm1.TextBox3.Value = "" Then
        MsgBox ("blah blah blah etc...")
        Exit Sub
End If
'Loop through TextBoxes and check for duplicates
Dim c As MSForms.Control
    For Each c In UserForm1.Controls
    
        If TypeOf c Is MSForms.TextBox Then
            'Need some code to check the vlaues and see if there are any duplicates
            'No values (blank boxes) do not count as duplicates
        End If
        
    Next c
    
End Sub
</SPAN></SPAN>
</SPAN></SPAN>

Thanks again.</SPAN></SPAN>
 
Upvote 0

Forum statistics

Threads
1,216,112
Messages
6,128,901
Members
449,477
Latest member
panjongshing

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