在 Excel VBA 中动态调整组合框的宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25250733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamically adjusting the width of a combobox in Excel VBA
提问by
I'm struggling to set up a combobox (in Excel VBA) in such a way that its width is automatically adjusted according to the length of the longest string that it contains.
我正在努力设置一个组合框(在 Excel VBA 中),使其宽度根据它包含的最长字符串的长度自动调整。
I'm trying to create a drop-down list (using a combobox in a form named "WorksheetSelectionForm") that, once a particular workbook is opened, appears on screen and allows the user to select which of the workbook's worksheets they wish to open.
我正在尝试创建一个下拉列表(使用名为“WorksheetSelectionForm”的表单中的组合框),一旦打开特定的工作簿,它就会出现在屏幕上,并允许用户选择他们希望打开的工作簿的哪个工作表.
I want the width of the combobox to adjust to the length of the longest text string in the drop-down list. Currently my drop-down list contains three items (the names of the worksheets that currently exist in the workbook). They are the following:
我希望组合框的宽度调整为下拉列表中最长文本字符串的长度。目前我的下拉列表包含三个项目(工作簿中当前存在的工作表的名称)。它们如下:
- "Profit and loss account" (23 characters)
- "Balance sheet" (13 characters)
- "Cashflow report" (15 characters)
- 《损益表》(23字)
- “资产负债表”(13 个字符)
- “现金流量报告”(15 个字符)
More worksheets could be added to the workbook and hence more items added to the drop-down list, hence why I don't want to simply fix the combobox's width at 23 points (the length of what's currently the longest string in the drop-down list).
更多的工作表可以添加到工作簿中,因此更多的项目添加到下拉列表中,因此我不想简单地将组合框的宽度固定为 23 点(下拉列表中当前最长字符串的长度)列表)。
I've been referring to the following thread from OzGrid for ideas (see entry #3): http://www.ozgrid.com/forum/showthread.php?t=55098. Their proposed solution is given below:
我一直在参考 OzGrid 的以下线程以获取想法(请参阅条目 #3):http://www.ozgrid.com/forum/showthread.php?t=55098 。他们提出的解决方案如下:
Dim iWidth As Double
ComboBox1.AutoSize = True
iWidth = 0
For i = 0 To ComboBox1.ListCount - 1
ComboBox1.ListIndex = i
If iWidth < ComboBox1.Width Then
iWidth = ComboBox1.Width
End If
Next
ComboBox1.Width = iWidth
ComboBOx1.AutoSize = False
ComboBox1.ListCount = 0
The problem with this solution is that the code ComboBox1.Width
in the if-then statement doesn't actually seem to work out the length of the combobox item that's currently in focus in the for-next loop.
此解决方案的问题在于ComboBox1.Width
if-then 语句中的代码实际上似乎并没有计算出当前在 for-next 循环中处于焦点的组合框项的长度。
Below is the code that I've written so far:
以下是我到目前为止编写的代码:
Private Sub Workbook_Open()
Dim Sheet As Worksheet, CmBox As MSForms.ComboBox, LWidth As Double, i As Integer
Set CmBox = WorksheetSelectionForm.ComboBox_Worksheets
LWidth = 0
'Populate the drop-down list with the names of the worksheets
For Each Sheet In Worksheets
CmBox.AddItem Sheet.Name
Next Sheet
'Find out the length of the longest string in the combobox
For i = 0 To CmBox.ListCount - 1
CmBox.ListIndex = i
If Len(CmBox.Value) > LWidth Then
LWidth = Len(CmBox.Value)
End If
Next i
'Set the combobox's width to the length of the longest string in the combobox
CmBox.ListWidth = LWidth
'Show the form on screen
WorksheetSelectionForm.Show
End Sub
This code, when run, doesn't seem to be setting the combobox's width as desired. It also generates a combobox that's missing all of its items (the names of the worksheets). Where have I gone wrong?
这段代码在运行时似乎没有根据需要设置组合框的宽度。它还会生成一个缺少所有项目(工作表名称)的组合框。我哪里错了?
Below is the code for when an item in the combobox is selected by the user (just in case it's of use to you):
以下是用户选择组合框中的项目时的代码(以防万一它对您有用):
Private Sub ComboBox_Worksheets_Change()
'Activate the worksheet whose name has been selected in the combobox
Sheets(ComboBox_Worksheets.Value).Activate
'Close the form
Unload WorksheetSelectionForm
End Sub
回答by Tom Collins
It's not too hard. It requires an API call. I've done it, but don't have the exact code with me, but this may do it. vb macro string width in pixel
这不是太难。它需要一个 API 调用。我已经完成了,但我没有确切的代码,但这可能会做到。以像素为单位的 vb 宏字符串宽度
If not, google these APIs:
如果没有,请谷歌这些 API:
- GetCharABCWidths (for true type fonts)
- GetChartABCWidthsFloat
- GetCharWidth (probably the most useful one)
- GetCharWidth32 (probably even more useful)
- GetCharWidthFloat
- GetCharABCWidths(用于真字体)
- 获取图表ABCWidthsFloat
- GetCharWidth(可能是最有用的一个)
- GetCharWidth32(可能更有用)
- 获取字符宽度浮点数
回答by Patrick Lepelletier
i used your code for a start base, and this is the outcome:
我用你的代码作为开始基地,这是结果:
Private Sub Workbook_Open()
Dim Sheet As Worksheet, CmBox As MSForms.ComboBox, LWidth As Double, i As Integer
dim Wb as workbook
load WorksheetSelectionForm
with WorksheetSelectionForm
Set CmBox = .ComboBox_Worksheets
'LWidth = 0
'Populate the drop-down list with the names of the worksheets
with cmBox
.clear
for each Wb in workbooks
For Each Sheet In WB.Worksheets 'i wasn't sure your way works for filling the list, did you verify it ?, so i do it my way
h = Sheet.Name
.AddItem h
if len(h)>Lwidth then LWidth = Len(h) 'no need to loop again when list is full
Next Sheet
next Wb
end with
'Find out the length of the longest string in the combobox
'For i = 0 To CmBox.ListCount - 1
' tmp_Length = len(CmBox.List(i)) 'this is an other way of doing it, without changing the cmBox value (could trigger events)
' If tmp_Length > LWidth Then
' LWidth = tmp_Length
' End If
'Next i
'Set the combobox's List's width to the length of the longest string in the combobox
CmBox.ListWidth = LWidth*8 'according to the list's Text Font size , you will need to adjust the *8
'Show the form on screen
.Show
end with
End Sub
结束子