Select value in listbox to be taken to worksheet value

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Morning,
I have a userform with values in a Listbox.
The code below is on a command button.

I would select a value in the Listbox & run the code on the command button to then be taken to that selection on the worksheet of which the range for it is J6 then down the page.
The code is nearly correct BUT doesnt select the value in column J for some reason.

Do you see why this might be.

Thanks

Rich (BB code):
Private Sub SelectAndGo_Click()
  With ThisWorkbook.Worksheets("DATABASE")
        Dim data As Variant
        data = .Range("J6:J" & .Cells(.Rows.Count, "J").End(xlUp).Row).Value
  End With
  
   Cells.Find(What:=ListBox1.Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False).Activate
  Unload HondaKeyCode
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
A couple of things.
Set "data" as a range instead of a variant. Also you don't want to look for the .value at the end (.value will give you what's in that cell vs the location of it)
You weren't telling the "find" where to look, it needs to be in the form of <range>.find. You had Cells.find, but Cells didn't refer to anything.
I also moved the dim statements first, above the With.

VBA Code:
Private Sub SelectAndGo_Click()
Dim data As Range

With ThisWorkbook.Worksheets("Sheet2")
    Set data = .Range("J6:J" & .Cells(.Rows.Count, "J").End(xlUp).Row)
End With
  
   data.Find(What:=ListBox1.Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False).Activate
 Unload HondaKeyCode
End Sub
 
Upvote 0
Hi,
Thats gives me a mismatch error & the below is shwon in yellow.


Rich (BB code):
   data.Find(What:=ListBox1.Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False).Activate
 Unload HondaKeyCode
 
Upvote 0
My bad. Change "Sheet2" to "DATABASE"
 
Upvote 0
Changing data.Find to Cells.Find theres no error message BUT it doesnt select in column J
 
Upvote 0
This works.


Rich (BB code):
Private Sub SelectAndGo_Click()
With ThisWorkbook.Worksheets("DATABASE")
Dim data As Variant

    data = .Range("J6:J" & .Cells(.Rows.Count, "J").End(xlUp).Row)
End With
  
   Cells.Find(What:=ListBox1.Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False).Activate
 Unload HondaKeyCode
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,779
Members
449,123
Latest member
StorageQueen24

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