Using "Tab" to move from cell to adjacent Combobox

GColeman

New Member
Joined
Feb 16, 2016
Messages
34
Office Version
  1. 2016
Platform
  1. Windows
I've looked this up and haven't found much luck on the subject, so I thought I'd try my luck here again.
The sheet I'm using has both data validation tables, and comboboxes. The comboboxes are all the same in configuration and properties. There are 50 of them, all in Column D. I went with Combobox because I wanted to predictive text feature for the list.
Everything works beautifully, almost. I can have the user select an item from the list, and move to the adjacent cell with the Tab button. But I need to start by having the Tab button move from Column C to the combobox in D, rather than the cell under the combobox. I also need it to activate the drop down list as soon as the user starts typing, thus the predictive text feature. (The selection must be an exact match to the list used for future functions). This is where I'm stumbling.
Here is what I'm using to exit the combobox.

Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 9 Then ActiveCell.Offset(0, 1).Select
End Sub

Ideas? Modify it to select an object, rather than (0,1)?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
With the following SelectionChange event handler, whenever you tab or arrow to a cell located in Column D, starting at D2, the combobox within that cell gets activated, if one exists. Note that you'll need to place this code in the code module for the sheet containing your comboboxes (right-click the sheet tab, and select View Code).

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim oOleObj As OLEObject
    If Target.Row > 1 And Target.Column = 4 Then
        For Each oOleObj In Me.OLEObjects
            If TypeName(oOleObj.Object) = "ComboBox" Then
                If oOleObj.TopLeftCell.Address = Target.Address Then
                    oOleObj.Activate
                    Exit Sub
                End If
            End If
        Next oOleObj
    End If
End Sub

Hope this helps!
 
Upvote 0
With the following SelectionChange event handler, whenever you tab or arrow to a cell located in Column D, starting at D2, the combobox within that cell gets activated, if one exists. Note that you'll need to place this code in the code module for the sheet containing your comboboxes (right-click the sheet tab, and select View Code).

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim oOleObj As OLEObject
    If Target.Row > 1 And Target.Column = 4 Then
        For Each oOleObj In Me.OLEObjects
            If TypeName(oOleObj.Object) = "ComboBox" Then
                If oOleObj.TopLeftCell.Address = Target.Address Then
                    oOleObj.Activate
                    Exit Sub
                End If
            End If
        Next oOleObj
    End If
End Sub

Hope this helps!


I appreciate the quick reply!

Unfortunately it seems to do nothing in this case. I am just getting into VBA and trying my best to understand the codes I use line by line. I want to be self sufficient when it comes to writing my own stuff. The only part I didn't quite get in this was "If oOleObj.TopLeftCell.Address = Target.Address Then". Would that not be used more for a range or a chart? I'm not doubting it at all, just trying to follow along and keep up.

Any other suggestions?
 
Upvote 0
Also, the cells directly before and after contain drop down lists populated with data validation. I'm not sure if that makes a difference, but the more info you have the better.
 
Upvote 0
Unfortunately it seems to do nothing in this case.

It should activate the combobox, thereby positioning the cursor within the combobox. Is this not what happens?

The only part I didn't quite get in this was "If oOleObj.TopLeftCell.Address = Target.Address Then". Would that not be used more for a range or a chart?

TopLeftCell returns a Range object that refers to the cell that lies beneath the upper left corner of the object, which in this case is the combobox. So you'll need to make sure that the upper left corner of each combobox does not extend beyond the cell. Is this the case?

Also, the cells directly before and after contain drop down lists populated with data validation. I'm not sure if that makes a difference, but the more info you have the better.

It shouldn't make a difference.
 
Upvote 0
Also, did you place the code in the code module for the sheet? For example, let's say that Sheet1 contains your comboboxes, you would right-click the sheet tab for Sheet1, select View Code, and copy/paste the code into the code module for the sheet. Have you done so?

Also, just to make sure, you said that Column D contains your comboboxes, correct?
 
Upvote 0
I'm not sure exactly how to put a screenshot in here, it wont let me paste. I made certain that the combobox is within the borders of cell D4. Positioning my curser in C4 and tabbing over selects D4, not the combobox as intended. I am on tab "Jan", right click, View code, Insert Module, paste. I get no response or error when I close and try the tab. I also tried it outside the module, just to be sure.
 
Upvote 0
I am on tab "Jan", right click, View code, Insert Module, paste.

It looks like this is where the problem lies. You should not be inserting a regular Module. When you right-click the sheet tab and select View Code, you will automatically be taken to the sheet module. Post the code there.

Does this help?
 
Upvote 0
It looks like this is where the problem lies. You should not be inserting a regular Module. When you right-click the sheet tab and select View Code, you will automatically be taken to the sheet module. Post the code there.

Does this help?


No, where I said to tried it outside the module that's what meant. Really not sure what I'm doing wrong. Also tried it in Workbook.

As a workaround, I taught myself how to put a user form in instead. Still a similar issue in that Tab doesn't select the comboboxes, but it's a lot more user friendly. But I'm still interested in finding a solution, I hate losing... ?
 
Upvote 0
I don't understand what you mean when you say...

where I said to tried it outside the module that's what meant

Did you make sure to copy the code into the sheet module, and not a regular module?
 
Upvote 0

Forum statistics

Threads
1,214,647
Messages
6,120,722
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top