Solved

combobox not adding value to cell after hitting enter

Posted on 2011-03-16
16
256 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
Comment Utility
>>>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
Comment Utility
You don't appear to set oldTarget anywhere - the selectionchange code refers to oldTarget2
0
 
LVL 3

Author Comment

by:fordraiders
Comment Utility
sorry it is old target2

Thanks
0
 
LVL 3

Author Comment

by:fordraiders
Comment Utility
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
Comment Utility
>>>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
Comment Utility
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
Comment Utility
You'll struggle to use the arrow keys to choose items!
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
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
Comment Utility
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
Comment Utility
>>>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
Comment Utility
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
Comment Utility
Ok great :)

So is your query solved?

Sid
0
 
LVL 3

Author Closing Comment

by:fordraiders
Comment Utility
Thanks
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now