VBA: How to highlight/select all text in a Combo Box?

richardcarter

Board Regular
Joined
Dec 10, 2003
Messages
77
I've developed a nice little macro that serves as a "search and go to" box. Its an ActiveX ComboBox which makes it easier to find and select products by their product code.

It works absolutely great.. except one thing (which is really frustrating me!). After I have completed a search, I would like to be able to highlight/select all the characters in the box. By doing that, I just need to type in the next product code to do another search. As it stands, after each search, a few characters at the end remain highlighted in the box which means I have the fiddly task of having to grab the cursor and select and highlght the code manually.. which is very fiddly.

So far, Ive tried various combinations of CodeSearch.SetFocus, CodeSearch.Activate and a host of others.. but nothing works.. Any ideas?

Private Sub CodeSearch_Change()

Res = CodeSearch.value
Set rng = Worksheets("Products").Range("ProductID")
With rng
Set MyChoice = .Find(What:=Res)
If Not MyChoice Is Nothing Then
Application.Goto MyChoice
ActiveWindow.ScrollRow = ActiveCell.Row

Else: GoTo ExitMyChoice
End If
End With
Exit Sub
ExitMyChoice:
MsgBox "Could Not Find " & Res
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try this in the same module:

Code:
Private Sub ComboBox1_GotFocus()
ComboBox1 = ""
End Sub
 
Upvote 0
Thanks for your reply.... the problem with using ---ComboBox1 = ""---- is that it automatically triggers the macro to find and go to a blank cell.
 
Upvote 0
I know this is an old thread, but I encountered a similar problem and I figured I should post the partial solution I found just in case someone stumbles across this.

Code:
Private Sub CodeSearch_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)


With CodeSearch
    .SelStart = 0
    .SelLength = Len(.Text)
End With


End Sub

This will highlight all the text in the ComboBox when you click it (which is a little less 'fiddly').
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,267
Members
449,075
Latest member
staticfluids

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