Solved

combobox not adding value to cell after hitting enter

Posted on 2011-03-16
16
261 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

776 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