J
James Duncan
Hi there,
I'm trying to make a combobox act like a normal Excel
cell. It's default property is set to Visible = False and
the control is located directly over cell D2. The
LinkedCell property is set to D2. When the user tabs (or
uses the right arrow) onto this cell the ComboBox becomes
visible and is set to active. I have also writen some
code that detects when Tab and Shift-Tab is pressed,
which moves the cursor onto the next or previous Excel
cell.
However, I also want the ComboBox control at this point
to become invisible. However, all my attempts to achieve
this have resulted in failure and worse than that it
crashes Excel. I assume this is because I'm trying to set
ComboBox1.Visible = False whilst the ComboBox is still
active/executing code?
I've tried every possible way I can think of to achieve
this, including Lost_Focus, etc. All result in Excel
crashing.
Please see my code below to see what I have achieved to
date, which isn't ideal. The control becomes invisible if
the user uses the mouse to click on any other cell or if
they press tab twice, etc.
Any help would be greatly appreciated.
Many thanks
James
Code follows:
Private Sub ComboBox1_KeyDown(ByVal KeyCode As
MSForms.ReturnInteger, ByVal Shift As Integer)
' required to prevent SelectChange from running,
which sets ComboBox1 to Visible = False,
' which crashes Excel
Application.EnableEvents = False
If KeyCode = 9 And Shift = 1 Then
ActiveCell.Offset(0, -1).Select
'ComboBox1.Visible = False
Else
If KeyCode = 9 Then
ActiveCell.Offset(0, 1).Select
'ComboBox1.Visible = False
End If
End If
Application.EnableEvents = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As
Range)
If Application.Intersect(Target, Range("D2
2")) Is
Nothing Then
If ComboBox1.Visible = True Then
ComboBox1.Visible = False
Else
ComboBox1.Visible = True
ComboBox1.Activate
If ComboBox1.ListCount > 0 And ComboBox1.Text
= "" Then
' select first item in the list - if i can
remember how!
End If
End If
End Sub
I'm trying to make a combobox act like a normal Excel
cell. It's default property is set to Visible = False and
the control is located directly over cell D2. The
LinkedCell property is set to D2. When the user tabs (or
uses the right arrow) onto this cell the ComboBox becomes
visible and is set to active. I have also writen some
code that detects when Tab and Shift-Tab is pressed,
which moves the cursor onto the next or previous Excel
cell.
However, I also want the ComboBox control at this point
to become invisible. However, all my attempts to achieve
this have resulted in failure and worse than that it
crashes Excel. I assume this is because I'm trying to set
ComboBox1.Visible = False whilst the ComboBox is still
active/executing code?
I've tried every possible way I can think of to achieve
this, including Lost_Focus, etc. All result in Excel
crashing.
Please see my code below to see what I have achieved to
date, which isn't ideal. The control becomes invisible if
the user uses the mouse to click on any other cell or if
they press tab twice, etc.
Any help would be greatly appreciated.
Many thanks
James
Code follows:
Private Sub ComboBox1_KeyDown(ByVal KeyCode As
MSForms.ReturnInteger, ByVal Shift As Integer)
' required to prevent SelectChange from running,
which sets ComboBox1 to Visible = False,
' which crashes Excel
Application.EnableEvents = False
If KeyCode = 9 And Shift = 1 Then
ActiveCell.Offset(0, -1).Select
'ComboBox1.Visible = False
Else
If KeyCode = 9 Then
ActiveCell.Offset(0, 1).Select
'ComboBox1.Visible = False
End If
End If
Application.EnableEvents = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As
Range)
If Application.Intersect(Target, Range("D2
![Big Grin :D :D](/styles/default/custom/smilies/grin.gif)
Nothing Then
If ComboBox1.Visible = True Then
ComboBox1.Visible = False
Else
ComboBox1.Visible = True
ComboBox1.Activate
If ComboBox1.ListCount > 0 And ComboBox1.Text
= "" Then
' select first item in the list - if i can
remember how!
End If
End If
End Sub