|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
aman1516 发表于 2014-4-26 13:49 
以下代码以工作表各列列宽做为列表框列宽的参照:
good idea !
为了确保输入的字符在列标题范围内,代码修如下:
Private Sub TextBox1_Change()
Dim i%, j%, c As Long, t$, s$, w$, wrr, arr, brr, crr
arr = [a1].CurrentRegion
If TextBox1.Text = "" Then
ListBox1.ColumnCount = UBound(arr, 2) - 1
ListBox1.List = arr
Else
For i = 1 To UBound(arr, 2) - 1
t = t & Cells(1, i).Value
If InStr(TextBox1.Text, Cells(1, i).Value) > 0 Then
s = s & ":" & i
w = w & Columns(i).ColumnWidth * 5 & ";"
End If
Next i
If InStr(t & ",", Mid(TextBox1.Text, Len(TextBox1.Text), 2)) = 0 Then TextBox1.Text = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
If s <> "" Then
s = Right(s, Len(s) - 1)
Else
For i = 1 To Cells(1, Cells.Columns.Count).End(xlToLeft).Column - 1
If InStr(Cells(1, i).Value, TextBox1.Text) > 0 Then s = i: w = Columns(i).ColumnWidth * 5 & ";": Exit For
Next i
End If
brr = Split(s, ":")
ReDim crr(1 To UBound(arr, 2), 1 To UBound(brr) + 1)
For i = 1 To UBound(arr, 2)
For j = 1 To UBound(brr) + 1
crr(i, j) = Cells(i, Val(brr(j - 1))).Value
Next j
Next i
With Me.ListBox1
.ColumnCount = UBound(brr) + 1
.ColumnWidths = w
.List = crr
End With
End If
End Sub
|
评分
-
1
查看全部评分
-
|