Hi,
I have a userform wherein I have a dropdown list. On selection of a value in that dropdown list, I want to search for that value in the worksheet and display the value of the corresponding cell in a textbox in the userform. For that, first I want to find that cell where searched value is, and then using the Offset property I would pick the value of the corresponding cell. Unfortunately, I am unable to find the text in the given range in the first place. Here's the code that I had attempted but no result is shown:
Please help.Dim searchTxt As String Dim wb As Workbook Dim ws As Worksheet Dim oRng, sRng As Range Dim finalRow As Long searchTxt = Trim(comboBox1.Value) Set wb = ThisWorkbook With wb Set ws = .Worksheets("Sheet1") End With finalRow = ws.Range("A1", "A65536").End(xlUp).Row Set sRng = ws.Range("B1:B" & finalRow) Set oRng = sRng.Find(What:=searchTxt, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=False) If Not oRng Is Nothing Then textBox1.Value = oRng Else MsgBox "No results found." End If
Peace,
Shivboy
Try to change the code marked in bold (if you need to find for data in column 'A' and take the value of column 'B' in the same row of found data):
Hope it's what you need.Dim searchTxt As String Dim wb As Workbook Dim ws As Worksheet Dim oRng, sRng As Range Dim finalRow As Long searchTxt = Trim(comboBox1.Value) Set wb = ThisWorkbook With wb Set ws = .Worksheets("foglio1") End With finalRow = ws.Cells(ws.Rows.Count, "a").End(xlUp).Row Set sRng = ws.Range("a1:a" & finalRow) Set oRng = sRng.Find(What:=searchTxt, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=False) If Not oRng Is Nothing Then textBox1.Value = oRng.Offset(0, 1).Value 'To take 'B' column value Else MsgBox "No results found." End If
Regards,
Antonio
Last edited by antoka05; 05-15-2007 at 08:21 AM.
Hi Antonio,
Thanks a lot for replying. I tried the code you suggested, but in vain. Although there is data in columns A & B, it still shows the oRng object as empty. Why is that happening? Please help.
Note: Check the If....Else condition.
Peace,
Shivboy
I'm sorry but in my previous code I wrote 'foglio1' rather than 'sheet1'. Please replace this code:
with:With wb Set ws = .Worksheets("foglio1") End With
If it still doesn't run please attach a little example of your file.With wb Set ws = .Worksheets("Sheet1") End With
Regards,
Antonio
What Antonio suggested should work If it isn't I have two suggestins to debug. Use msgbox to check if your combobox is actually returning the value you are expecting. egOriginally Posted by antoka05
If the active cell is beyond the cell where you know the value exists then search may not find the cell as search starts from the active cell. Might be a good idea to activate the first cell of the range before you start search eg addsearchTxt = Trim(comboBox1.Value) msgbox searchTxt ...
Also I noticed a slight descripancy. What you did was decide last row of the range by taking last cell of col A, search col B for the data and write thisRange("B1").Activate Set oRng = sRng.Find....
data to text box. Antonio is doing a little different. He is also taking the last used cell of col A to decide the finalrow of the range but the range he is searching for data is col A and if he finds the data in any cell in col A he takes data from corresponding cell in col B and writes to text box. Just check what is your actual requirement.
A V Veerkar
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks