Create a search database with return results to another cell.

akshay0880

New Member
Joined
Jul 11, 2021
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hey Guys,

I am trying to build sort of a database with a search in it.

So, what i am trying to do is:
When user enters a text in C7 and hit the button, it searches the text (exact match) in Sheet2(called Data) then displays it in Cell C19.
Attached screenshots for more clarity.
Thanks a lot in advance :)
 

Attachments

  • Sheet1-Data.png
    Sheet1-Data.png
    40.2 KB · Views: 16
  • Sheet1-Search.png
    Sheet1-Search.png
    34.8 KB · Views: 16

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Thank you for the screen shots, but can you please clarify the nature of the data, because to read your message it seems from the Search sheet you want to enter text in C7, match it on the Data sheet, but then display the same text that was entered in C19? I'm just unsure of the point of the matching function.

Is this a lookup situation, like the item to match is in Column C with the return value being in Column D? Because if that is the case, I would suggest merely using a lookup formula in Cell C19, like:

Excel Formula:
=XLOOKUP($C$7,Data!C:C,Data!D:D,"",0)

and you could format the cell so that it doesn't display 0 values for non-matches.
 
Upvote 1
Hello, Thank you for replying. I found out a code that is currently working.

VBA Code:
Sub Find()
Dim rng As Range
Dim Errors As String
Dim rownumber As Long

Errors = Sheet1.Cells(7, 3)


Set rng = Sheet2.Columns("A:A").Find(What:=Errors, _
    LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    rownumber = rng.Row
    Sheet1.Cells(19, 3).Value = Sheet2.Cells(rownumber, 2).Value

End Sub


I do have one more thing to add and not sure how to do this, its basically a message box.
so if I am typing something that is not in Sheet2, then i want it to say something like "not found'.
Right now if I do so, I am getting a 'run-time error '91': Object variable or With block variable not set'

Can you help please?
 

Attachments

  • onError.png
    onError.png
    61.9 KB · Views: 8
Upvote 0
Hello, Thank you for replying. I found out a code that is currently working.

I do have one more thing to add and not sure how to do this, its basically a message box.
so if I am typing something that is not in Sheet2, then i want it to say something like "not found'.
Right now if I do so, I am getting a 'run-time error '91': Object variable or With block variable not set'

Can you help please?

The reason you get the error is because the macro is trying to extract the row number of something it cannot find.

I think that if you have to use a macro, you should use a LOOKUP formula (either VLOOKUP or XLOOKUP). Please try this macro:

VBA Code:
Sub LookItUp()


With Sheets("Search").Range("C19")

  'Comment Out Whichever Formula You Prefer
  .FormulaR1C1 = "=IFERROR(VLOOKUP(R7C3,Data!C1:C2,2,0),""Not found"")"
  
  .FormulaR1C1 = "=XLOOKUP(R7C3,Data!C1,Data!C2,""Not found"",0)"

End With


End Sub
 

Attachments

  • MrExcel0314_Search_Error.png
    MrExcel0314_Search_Error.png
    6.9 KB · Views: 8
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

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