在Excel VBA中动态调整组合框的宽度

时间:2014-08-11 19:18:00

标签: excel-vba combobox width vba excel

我正在努力设置一个组合框(在Excel VBA中),其宽度会根据其包含的最长字符串的长度自动调整。

我尝试创建一个下拉列表(使用名为" WorksheetSelectionForm&#34的表单中的组合框),一旦打开特定工作簿,就会出现在屏幕上并允许用户选择他们希望打开哪个工作簿的工作表。

我希望组合框的宽度调整为下拉列表中最长文本字符串的长度。目前,我的下拉列表包含三个项目(工作簿中当前存在的工作表的名称)。它们如下:

  • "损益账户" (23个字符)
  • "资产负债表" (13个字符)
  • "现金流量报告" (15个字符)

可以在工作簿中添加更多工作表,因此会在下拉列表中添加更多项目,因此我不想简单地将组合框的宽度修复为23点(什么是&的长度) #39; s当前是下拉列表中最长的字符串。)

我一直在引用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 

这个解决方案的问题在于,if-then语句中的代码ComboBox1.Width实际上似乎没有计算出目前在for中关注的组合框项目的长度-next loop。

以下是我到目前为止编写的代码:

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

此代码在运行时似乎并未根据需要设置组合框的宽度。它还会生成一个组合框,它缺少所有项目(工作表的名称)。我哪里出错?

以下是用户选择组合框中的项目时的代码(以防万一它对您有用):

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

2 个答案:

答案 0 :(得分:1)

这不是太难。它需要API调用。我已经完成了,但没有我的确切代码,但这可能会这样做。 vb macro string width in pixel

如果没有,请谷歌这些API:

  • GetCharABCWidths(对于真实字体)
  • GetChartABCWidthsFloat
  • GetCharWidth(可能是最有用的)
  • GetCharWidth32(可能更有用)
  • GetCharWidthFloat

答案 1 :(得分:0)

我使用你的代码作为开始基础,这就是结果:

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