Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlight text in combobox

Status
Not open for further replies.

yoshi88

Technical User
Mar 18, 2001
48
0
0
CA
Hi, I am using a code that use a combo box in excel when there is a validation list in the cell. Everything works perfectly except one thing. When the code is done the cursor in the combobox is at the end of the text. I would prefer the text to be highlighted so I dont have to select it by myselft or to do multiple backspace to clear the field if necessary. Can someone help me with this issue?

Here is the code

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
On Error GoTo errHandler

If Target.Count > 1 Then GoTo exitHandler

Set cboTemp = ws.OLEObjects("TempCombo")
On Error Resume Next
If cboTemp.Visible = True Then
With cboTemp
.Top = 10
.Left = 10
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
End If

On Error GoTo errHandler
If Target.Validation.Type = 3 Then
'if the cell contains a data validation list
Application.EnableEvents = False
'get the data validation formula
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 15
.Height = Target.Height + 5
.ListFillRange = ws.Range(str).Address
.LinkedCell = Target.Address
End With
cboTemp.Activate

End If

exitHandler:
Application.ScreenUpdating = True
Application.EnableEvents = True
Call Cost
Exit Sub
errHandler:
Resume exitHandler

End Sub


Thanks

Francois
 
This should be achieved by using the following

Code:
cboTemp.SelStart = 0
cboTemp.SelLength = Me.ComboBox1.TextLength


Hope this helps.

Matt
[rockband]
 
Sorry should have read
Code:
cboTemp.SelStart = 0
cboTemp.SelLength = cboTemp.TextLength


Hope this helps.

Matt
[rockband]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top