I was wondering if this is possible to do or not. I am pretty sure it’s not, but wanted to see if it was and someone else might know how. I am using Excel 2010 and everything is being done with VBA. What I would like to do is set a ComboBox with a minimum width size and then AutoSize if typed data becomes longer than the length of the ComboBox.

OK, so let me clarify some misconceptions or questions that I am going to get asked about, such as why I would want to allow users to type in a ComboBox. On another sheet, apart from the sheet that the ComboBox is on, I have a list of items. As items are typed in here the ComboBox is populated with the new item, I also have it sorting the list in alphabetical order and it spell checks them too, as they are being added.

I then have the code to also check for the longest length of text in each cell and the ComboBox will size to the longest length of text within its list, so it can fully be read in the dropdown.

I also want users to be able to type in the ComboBox for items that are not in the list already. The list is for just the most common items used, but often other miscellaneous stuff needs to be entered. I don’t want the miscellaneous stuff added to the list within the ComboBox, so I just want the user to be able to type in data within the ComboBox, but if it is used a lot they can add it to the list on the other sheet.

In case someone is wondering, I do have spellcheck working for the ComboBox when users type data in. All I did was make the cell that the ComboBox is over equal to the text in the ComboBox when the ComboBox is deactivated (LostFocus), then I spellcheck that cell, followed by making the ComboBox equal to the cell that was just spellchecked.

Anyway, as already mentioned I do have the ComboBox’s width set to the longest item in the list using code. Is there a way that I can somehow keep the ComboBox’s minimum width set to this size, but then the width will increase when the user is typing data in that becomes longer than what the box is currently set to? I am guessing this would be easy to do if I was to adjust it after the user finished typing (when the ComboBox LostFocus), but is it possible to do this while they are typing? Setting the AutoSize will not work, because it will ignore the size of the longest item in the list, and shrink to almost nothing, making it impossible to see the items in the list. The other issue with AutoSize is that you can’t read items in the list as you are typing. Like when you are typing it will prefill items from the list as you are typing, but the prefilled items can’t be read when using AutoSize.

有帮助吗?

解决方案

I guess it is possible to do, I just figured it out. The iLength is what determines the default minimum width size should be, which is slightly larger than the size of a column in a different sheet where the ComboBox is pulling its data from. I just did some basic calculations to figure that the length (character count) of text being typed in the notes field is about 4.5 times smaller than the default length. Of course, the 4.5 would change if I used a different sized font. If the typed text (based on the calculation) becomes greater than the default size, I set the ComboBox to AutoSize, otherwise AutoSize was set to False. Then when I cleared the data from the field I just have the code in the clear button to reset the ComboBox back to the default size and to set AutoSize to False. This works perfectly.

Private Sub cboNotes_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

Dim iLength As Integer

iLength = Sheets("Notes").Range("A1").Columns.Width + 20

If Len(cboNotes.Value) * 4.5 < iLength Then
    cboNotes.Width = Sheets("Notes").Range("A1").Columns.Width + 20
    cboNotes.AutoSize = False
    Exit Sub
Else
    cboNotes.AutoSize = True
    Exit Sub
End If

End Sub
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top