Excel 调整列表框。高度问题
我有一个带有文本框的用户表单和一个带有以下计划的列表框:Excel 调整列表框。高度问题,excel,vba,listbox,height,Excel,Vba,Listbox,Height,我有一个带有文本框的用户表单和一个带有以下计划的列表框: 用户在Textbox1中输入文本 每次Textbox1.Text更改时,都会执行具有以下功能的搜索: Textbox1.在工作表的特定范围内搜索文本 Textbox1.可以多次找到文本 Listbox1将填充搜索结果 到目前为止还不错。由于有大量数据,列表可以获得许多项。在这种情况下,列表超出了屏幕,我必须限制Listbox1.Height。这是上面的代码: Private Sub TextBox1_Change() Dim
Textbox1
中输入文本Textbox1.Text
更改时,都会执行具有以下功能的搜索:
Textbox1.在工作表的特定范围内搜索文本
Textbox1.可以多次找到文本
将填充搜索结果Listbox1
Listbox1.Height
。这是上面的代码:
Private Sub TextBox1_Change()
Dim srchWord As String, firstAddress As String
Dim srchRng As Range, cell As Range
Dim maxRow As Integer
ListBox1.Clear
If TextBox1.Value = "" Then
ListBox1.Height = 0
Else
With ThisWorkbook.Worksheets(1)
maxRow = .Cells(.Rows.Count, 2).End(xlUp).Row
Set srchRng = .Range("A2:A" & maxRow)
End With
srchWord = TextBox1.Value
Set cell = srchRng.Find(srchWord, LookIn:=xlValues, lookat:=xlPart)
With ListBox1
If Not cell Is Nothing Then
firstAddress = cell.Address
Do
If Not cell.Value Like "*(*" Then
.AddItem (cell.Value)
Select Case .ListCount
Case Is < 2
.Height = 17
Case Is < 21
.Height = 15 * .ListCount
Case Else
.Height = 272.5
End Select
Me.Height = 500
End If
Set cell = srchRng.FindNext(cell)
Loop While Not cell.Address = firstAddress
End If
End With
End If
End Sub
但这也将Listbox1.Height设置为单个项目的此值。(右侧有箭头)
有人知道我到底该如何控制Listbox1.高度而不出现这些不必要的行为吗?此外,如果有人能提出另一种结构,可以遵循最初提到的计划,我愿意放弃列表框。这似乎是一种未完全探索的行为
- 根据我的经验,我只是重新定义了一些列表框参数
- 尝试将建议的
设置为False和True.IntegralHeight
- 在某些情况下,另一种可能的测量方法会有所帮助:尝试为列表框选择接近以下乘法的高度:
后面插入以下代码:
With ListBox1
.Top = 18 ' << redefine your starting Point
.Font.Size = 10 ' << redefine your font size
.IntegralHeight = False ' << try the cited recommendation :-)
希望有帮助
链接
请访问T.M.@T.M.:感谢您的快速响应和宝贵时间。你的回答给了我我想要的,这就是为什么我要这样做。我发布这篇文章只是为了将来参考
我最终为实施计划所做的。
- 首先,我插入:
这个
还有这个
.Height = .Height + .Font.Size + 2
.IntegralHeight = True
End With
我按照你的建议用.Font.Size
链接了.Height
。只要不需要为高度指定绝对值,就不需要在我的代码中使用Select Case
语句
- 此外,我意识到没有必要在每次添加项目时更改高度,而只是在过程结束时更改,因此我将其从循环中删除
- 最后,我添加了一段代码,当Textbox1为空时,该代码将使列表不可见。代码如下所示:
最终用户表单代码:
Option Compare Text
Option Explicit
Private bsdel As Boolean 'indicates if backspace or delete keys have been hit.
Private Sub ListBox1_Click()
Dim cell As Range
Dim maxRow As Integer
With ThisWorkbook.Worksheets(1)
maxRow = .Cells(.Rows.Count, 2).End(xlUp).Row
Set cell = .Range("A1:A" & maxRow).Find(UserForm11.ListBox1.Text, LookIn:=xlValues, lookat:=xlWhole)
If Not cell Is Nothing Then
cell.Select
'do other stuff also.
End If
End With
End Sub
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
bsdel = False
If KeyCode = 8 Or KeyCode = 46 Then _
bsdel = True
End Sub
Private Sub TextBox1_Change()
Dim srchWord As String, firstAddress As String
Dim srchRng As Range, cell As Range
Dim maxRow As Integer
ListBox1.Clear
ListBox1.Visible = True
If bsdel And TextBox1.Value = "" Then
ListBox1.Visible = False
Me.Height = 130
Else
With ThisWorkbook.Worksheets(1)
maxRow = .Cells(.Rows.Count, 2).End(xlUp).Row
Set srchRng = .Range("A1:A" & maxRow)
End With
srchWord = TextBox1.Value
Set cell = srchRng.Find(srchWord, LookIn:=xlValues, lookat:=xlPart)
With ListBox1
'.Top = 84 'test made: deleting this made no difference.
'.Font.Size = 10 'test made: deleting this made no difference.
.IntegralHeight = False
If Not cell Is Nothing Then
firstAddress = cell.Address
Do
If Not cell.Value Like "*(*" Then 'this range includes notes within parenthesis and I didn't need them.
.AddItem (cell.Value)
End If
Set cell = srchRng.FindNext(cell)
Loop While Not cell.Address = firstAddress
If .ListCount < 21 Then 'the size is adjusted.
.Height = (.Font.Size + 2) * .ListCount
Else 'the size stays fixed at maximum.
.Height = (.Font.Size + 2) * 20
End If
End If
Me.Height = .Height + 130
.Height = .Height + .Font.Size + 2
.IntegralHeight = True
End With
End If
bsdel = False
End Sub
Private Sub UserForm_Activate()
TextBox1.SetFocus
End Sub
Private Sub UserForm_Initialize()
ListBox1.Visible = False
End Sub
选项比较文本
选项显式
Private bsdel As Boolean'表示是否命中了退格键或删除键。
私有子列表框1_单击()
暗淡单元格作为范围
Dim maxRow作为整数
使用此工作簿。工作表(1)
maxRow=.Cells(.Rows.Count,2).End(xlUp).Row
设置单元格=.Range(“A1:A”&maxRow).Find(UserForm11.ListBox1.Text,LookIn:=xlValues,lookat:=xlWhole)
如果不是的话,那细胞就什么都不是了
单元格。选择
“还要做其他事情。
如果结束
以
端接头
私有子文本框1u KeyDown(ByVal键代码为MSForms.ReturnInteger,ByVal移位为Integer)
bsdel=False
如果KeyCode=8或KeyCode=46,则_
bsdel=True
端接头
专用子文本框1_Change()
Dim srchWord作为字符串,FIRSTMADDRESS作为字符串
尺寸srchRng作为范围,单元格作为范围
Dim maxRow作为整数
列表框1。清除
ListBox1.Visible=True
如果bsdel和TextBox1.Value=”“,则
ListBox1.Visible=False
身高=130
其他的
使用此工作簿。工作表(1)
maxRow=.Cells(.Rows.Count,2).End(xlUp).Row
设置srchRng=.Range(“A1:A”&maxRow)
以
srchWord=TextBox1.Value
Set cell=srchRng.Find(srchWord,LookIn:=xlValues,lookat:=xlPart)
使用ListBox1
'.Top=84'已做测试:删除此项没有任何区别。
“.Font.Size=10”已做测试:删除此项没有任何区别。
.高度=假
如果不是的话,那细胞就什么都不是了
firstAddress=单元格地址
做
如果不是像“*(*”这样的cell.Value,那么“这个范围包括括号内的注释,我不需要它们。
.AddItem(单元格值)
如果结束
设置单元格=srchRng.FindNext(单元格)
非单元格时循环。地址=第一个地址
如果.ListCount<21,则调整大小。
.Height=(.Font.Size+2)*.ListCount
否则,大小将保持固定在最大值。
.Height=(.Font.Size+2)*20
如果结束
如果结束
Me.Height=.Height+130
.Height=.Height+.Font.Size+2
.积分高度=真
以
如果结束
bsdel=False
端接头
私有子用户表单_Activate()
TextBox1.SetFocus
端接头
私有子用户表单_初始化()
ListBox1.Visible=False
端接头
很高兴您能够克服列表框/复选框行为描述不当的问题。
With ListBox1
.Top = 18
.Font.Size = 10
.IntegralHeight = False
.Height = .Height + .Font.Size + 2
.IntegralHeight = True
End With
Option Compare Text
Option Explicit
Private bsdel As Boolean 'indicates if backspace or delete keys have been hit.
Private Sub ListBox1_Click()
Dim cell As Range
Dim maxRow As Integer
With ThisWorkbook.Worksheets(1)
maxRow = .Cells(.Rows.Count, 2).End(xlUp).Row
Set cell = .Range("A1:A" & maxRow).Find(UserForm11.ListBox1.Text, LookIn:=xlValues, lookat:=xlWhole)
If Not cell Is Nothing Then
cell.Select
'do other stuff also.
End If
End With
End Sub
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
bsdel = False
If KeyCode = 8 Or KeyCode = 46 Then _
bsdel = True
End Sub
Private Sub TextBox1_Change()
Dim srchWord As String, firstAddress As String
Dim srchRng As Range, cell As Range
Dim maxRow As Integer
ListBox1.Clear
ListBox1.Visible = True
If bsdel And TextBox1.Value = "" Then
ListBox1.Visible = False
Me.Height = 130
Else
With ThisWorkbook.Worksheets(1)
maxRow = .Cells(.Rows.Count, 2).End(xlUp).Row
Set srchRng = .Range("A1:A" & maxRow)
End With
srchWord = TextBox1.Value
Set cell = srchRng.Find(srchWord, LookIn:=xlValues, lookat:=xlPart)
With ListBox1
'.Top = 84 'test made: deleting this made no difference.
'.Font.Size = 10 'test made: deleting this made no difference.
.IntegralHeight = False
If Not cell Is Nothing Then
firstAddress = cell.Address
Do
If Not cell.Value Like "*(*" Then 'this range includes notes within parenthesis and I didn't need them.
.AddItem (cell.Value)
End If
Set cell = srchRng.FindNext(cell)
Loop While Not cell.Address = firstAddress
If .ListCount < 21 Then 'the size is adjusted.
.Height = (.Font.Size + 2) * .ListCount
Else 'the size stays fixed at maximum.
.Height = (.Font.Size + 2) * 20
End If
End If
Me.Height = .Height + 130
.Height = .Height + .Font.Size + 2
.IntegralHeight = True
End With
End If
bsdel = False
End Sub
Private Sub UserForm_Activate()
TextBox1.SetFocus
End Sub
Private Sub UserForm_Initialize()
ListBox1.Visible = False
End Sub