Search for Data already Exists

annadinesh

Board Regular
Joined
Mar 1, 2017
Messages
105
Dear Experts

i have 2 sheets
1 Data Entry
2 Database

in Database there are 4 coloums Name Dealer Code, Claim Number, Claim Date and Claim Amount
in Data Entry there is 2 Cells with heading Dealer Name and Claim Number and a command Button Name "Add to Database"

I want if i enter data on both Cells and while Save it should search both value in Sheet Database on Same Row if Dealer Name and Claim Number found already exists on Same Row then a Msg Shows Dealer Name and Claim Number already Exists.


Thank you
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

It would also be helpful to have a small set of sample data for both sheets with XL2BB so that we can copy for testing (as well as see just what you have and where it is) and explain your requirement with reference to the sample data.
 
Last edited:
Upvote 0
Which two cells are used on Data Entry sheet?
 
Upvote 0
Test this on a copy of your Workbook as unexpected results may occur and Macro actions can't be undone. Paste this in the same Sub with your Button before any other code.
VBA Code:
Dim wb As Workbook, dSht As Worksheet, eSht As Worksheet, cNum, dName, lRow As Long, fRng As Range, lRng As Range
Set wb = ThisWorkbook: Set dSht = wb.Sheets("Database"): Set eSht = wb.Sheets("Data Entry")
lRow = dSht.Columns("B").Rows(dSht.Rows.Count).End(xlUp).Row
Set fRng = dSht.Range("B2:B" & lRow)
dName = eSht.Range("A1"): cNum = eSht.Range("B1")
Set lRng = fRng.Find(cNum.Value, LookAt:=xlWhole)
If Not lRng = "" Then
    If lRng.Offset.Value = dName.Value Then
        MsgBox "Claim already exists.", vbInformation, "Duplicate Claim"
    Exit Sub
    End If
End If
 
Upvote 0
@annadinesh
If your question is not resolved by the above code, please review post #2
In any case, please review the first paragraph of that post. It is your interests for helpers to have information that best lets them help you. ;)
 
Upvote 0
Test this on a copy of your Workbook as unexpected results may occur and Macro actions can't be undone. Paste this in the same Sub with your Button before any other code.
VBA Code:
Dim wb As Workbook, dSht As Worksheet, eSht As Worksheet, cNum, dName, lRow As Long, fRng As Range, lRng As Range
Set wb = ThisWorkbook: Set dSht = wb.Sheets("Database"): Set eSht = wb.Sheets("Data Entry")
lRow = dSht.Columns("B").Rows(dSht.Rows.Count).End(xlUp).Row
Set fRng = dSht.Range("B2:B" & lRow)
dName = eSht.Range("A1"): cNum = eSht.Range("B1")
Set lRng = fRng.Find(cNum.Value, LookAt:=xlWhole)
If Not lRng = "" Then
    If lRng.Offset.Value = dName.Value Then
        MsgBox "Claim already exists.", vbInformation, "Duplicate Claim"
    Exit Sub
    End If
End If
ERROR

1701065134003.png
 
Upvote 0
Book1.xlsm
ABCDEFGH
1E1202a003
2
3
4
Data Entry



Book1.xlsm
ABCDE
1DEALER CODECLAIM NUMBERCLAIM DATECLAIM AMOUNT
2E1202a00327.11.202366
3E3406a00227.11.202355
4E1202a00327.11.202366
5E1207a00127.11.202370
6E1202a00127.11.202362
7E3406a00327.11.2023333
8E1202a00527.11.202336
9E1207a00327.11.2023693
10E1202a00327.11.202333
11E3406
Database




IF same data - Claim Number with Same Dealer Code found then it should show an msg
 
Upvote 0
Thanks for the XL2BB sample data (y)
But we still did not get your Excel version listed in your profile. (n)

Do you really need vba? Could you just use a formula something like this?

annadinesh.xlsm
ABC
1E1202a003Dealer Name and Claim Number already Exists.
Data Entry
Cell Formulas
RangeFormula
C1C1=IF(COUNTIFS(Database!A$1:A$1000,A1,Database!B$1:B$1000,B1),"Dealer Name and Claim Number already Exists.","")
 
Upvote 0
Solution

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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