combobox not adding value to cell after hitting enter

excel 2003 vba
floating combobox on column C

tHE COMBOBOX is being loaded via values from sheet range


OLD REFERENCE QUESTION:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_21715939.html?sfQueryTermInfo=1+10+30+combox+fordraid


Problem
after selecting a value from the combobox and hitting enter, the value is not being stored in the cell
plus I want to just type in the cell and have the value saved also..(but this currently works)

code events for combobox1
Option Explicit
Public oldTarget As Range

sheet1 events


Private Sub ComboBox1_Change()
If Not oldTarget Is Nothing Then oldTarget = ComboBox1.Value
ComboBox1.Height = ActiveCell.RowHeight - 6
ComboBox1.Width = 148
End Sub

Private Sub ComboBox1_Click()
On Error Resume Next
If Not oldTarget Is Nothing Then oldTarget = ComboBox1.Value
ComboBox1.Height = ActiveCell.RowHeight - 6
ComboBox1.Width = 148
End Sub


Private Sub ComboBox1_GotFocus()
ComboBox1.DropDown
ComboBox1.Height = ActiveCell.RowHeight - 6
ComboBox1.Width = 148
End Sub

Private Sub Worksheet_SelectionChange(ByVal target As Range)

Dim CutCopyMode As XlCutCopyMode
    
    If PreviouslySelectedRange Is Nothing Then
        Set PreviouslySelectedRange = target
        Application.CutCopyMode = False
    End If

    CutCopyMode = Application.CutCopyMode

    ComboBox1.Top = ActiveCell.Top
    ComboBox1.Top = target.Top
   
    'ComboBox1 value (if changed) will be stored in same row as target, column c
    Set oldTarget2 = Cells(target.Row, 3)
    ComboBox1.Height = ActiveCell.RowHeight - 6
    ComboBox1.Width = 148

    Select Case CutCopyMode
        Case xlCopy:
            PreviouslySelectedRange.Copy
        Case xlCut:
            PreviouslySelectedRange.Cut
        Case Else:
            Set PreviouslySelectedRange = target
    End Select
    
    ' get out of column Headers
    If target.Rows.Count = Cells.Rows.Count Then Exit Sub
    
    If Not Intersect(target, [A1:AU1]) Is Nothing Then
        MsgBox "Not Allowed To Edit!", vbCritical, "Template Protection Error"
        Range("A2").Select
    End If

End Sub

Open in new window

LVL 3
FordraidersAsked:
Who is Participating?
 
SiddharthRoutCommented:
Yes, like I mentioned above that I have not done the error handling.

What is the user selects a cell in Col A and then presses enter in the combo? The values will be written in Col A

So you might want to add this to the above code.

Private Sub ComboBox1_Click()
    If ActiveCell.Column <> 3 Then
        MsgBox "Please select a cell in Column C and try again."
        Exit Sub
    End If
    
    On Error Resume Next
    If Not oldTarget Is Nothing Then oldTarget = ComboBox1.Value
    On Error GoTo 0
    
    ComboBox1.Height = ActiveCell.RowHeight - 6
    ComboBox1.Width = 148
    
    ActiveCell.Value = ComboBox1.Text
    ActiveCell.Offset(1).Select
End Sub

Open in new window


Sid
0
 
SiddharthRoutCommented:
>>>after selecting a value from the combobox and hitting enter, the value is not being stored in the cell

I haven't seen the excel file but if say you want to store the value of the CB to Say Cell A1 on Enter key then include this in you code

Private Sub ComboBox1_KeyDown(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 13 Then Range("A1").Value = ComboBox1.Text
End Sub

Open in new window


Sid
0
 
Rory ArchibaldCommented:
You don't appear to set oldTarget anywhere - the selectionchange code refers to oldTarget2
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
FordraidersAuthor Commented:
sorry it is old target2

Thanks
0
 
FordraidersAuthor Commented:
sid,
The combobox stays in column C  and any selction goes into the cell currently active. in ccolumn C.

0
 
SiddharthRoutCommented:
>>>>any selction goes into the cell currently active. in ccolumn C.

Try this then. I haven't added error handling in this.

Private Sub ComboBox1_KeyDown(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 13 Then Selection.Value = ComboBox1.Text
End Sub

Open in new window


Sid
0
 
FordraidersAuthor Commented:
ok...
I changed it to:
If KeyCode = 13 Then ActiveCell.Value = ComboBox1.Text

but then after hiitting enter. I need to move to next cell down ?

Thanks
0
 
SiddharthRoutCommented:
Untested

Try this

Private Sub ComboBox1_KeyDown(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 13 Then ActiveCell.Value = ComboBox1.Text
    ActiveCell.Offset(1).Select
End Sub

Open in new window


Sid
0
 
FordraidersAuthor Commented:
sid , it worked and I also added

Private Sub ComboBox1_Click()
On Error Resume Next
If Not oldTarget Is Nothing Then oldTarget = ComboBox1.Value
ComboBox1.Height = ActiveCell.RowHeight - 6
ComboBox1.Width = 148
ActiveCell.Value = ComboBox1.Text    <------  so if a person clicks a choice while in the box  it takes..
    ActiveCell.Offset(1).Select
End Sub

unless you forsee a problem doing that ?

Thanks
fordraiders
0
 
Rory ArchibaldCommented:
You'll struggle to use the arrow keys to choose items!
0
 
SiddharthRoutCommented:
OOPs.. Change that Click event to ComboBox1_KeyDown. I didn't see that you changed that. Nice catch Rory. :)

Sid
0
 
FordraidersAuthor Commented:
rorya, yep, its a killer...
sid, I have nothing to do in Column A...so column A is not a problem...for reference..

so take the code in:
ID:35156433

and put it in
ComboBox1_KeyDown
?/
Thanks
0
 
SiddharthRoutCommented:
>>>sid, I have nothing to do in Column A...so column A is not a problem...for reference..

Agreed but what if you accidentally selected that ;)

Hence it is advisable to add that extra check as I did in ID: 35156433

Sid
0
 
FordraidersAuthor Commented:
the combobox nevers moves out of column C..it stays there...
no matter where my cursor is (ROW) the combobox moves with me but stays in column C
0
 
SiddharthRoutCommented:
Ok great :)

So is your query solved?

Sid
0
 
FordraidersAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.