Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

combobox not adding value to cell after hitting enter

Posted on 2011-03-16
16
Medium Priority
?
276 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:fordraiders
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 2
16 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35154200
>>>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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35154772
You don't appear to set oldTarget anywhere - the selectionchange code refers to oldTarget2
0
 
LVL 3

Author Comment

by:fordraiders
ID: 35156104
sorry it is old target2

Thanks
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Author Comment

by:fordraiders
ID: 35156112
sid,
The combobox stays in column C  and any selction goes into the cell currently active. in ccolumn C.

0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35156125
>>>>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
 
LVL 3

Author Comment

by:fordraiders
ID: 35156221
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
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35156238
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
 
LVL 3

Author Comment

by:fordraiders
ID: 35156388
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
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 2000 total points
ID: 35156433
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35156437
You'll struggle to use the arrow keys to choose items!
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35156462
OOPs.. Change that Click event to ComboBox1_KeyDown. I didn't see that you changed that. Nice catch Rory. :)

Sid
0
 
LVL 3

Author Comment

by:fordraiders
ID: 35156643
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
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35156664
>>>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
 
LVL 3

Author Comment

by:fordraiders
ID: 35158350
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
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35162725
Ok great :)

So is your query solved?

Sid
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 35181256
Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question