|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Private Original$ '存储目标单元格原始值
- Private ListIdx& '存储列表框按下方向键而尚未弹起时的行索引值
- Private Const RangeAddr = "E4" '设置作用区域
- Private Sub HideCtrl()
- '隐藏控件,到处需要使用,做出公共过程
- ListBox1.Clear '清空列表
- TextBox1 = "" '文本框为空
- ListBox1.Visible = False '隐藏列表框
- TextBox1.Visible = False '隐藏文本框
- End Sub
- Private Sub ListBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
- '为什么要记录ListIdx?是因为在ListBox1按住上、下箭头无需弹起也能滚动列表(持续触发KeyDown)
- '不在按下方向键时标记ListIdx,KeyUp按键弹起事件中就会多走一定数量的行。
- ListIdx = ListBox1.ListIndex
- End Sub
- Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
- ListIdx = ListBox1.ListIndex
- End Sub
- Private Sub Ctrl_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
- '因为Listbox和TEXTBOX控件都要用到这个事件,故定义一个公共子过程
- 'Shift值:0代表没有按3键中的任何一个,1代表按了Shift键,2代表按了Ctrl键,4代表按了ALT
- Dim lRow&, lCol&, i&
- With ListBox1
- Select Case KeyCode
- Case vbKeyReturn '按回车键,完成输入动作
- WriteInto
-
- Case vbKeyTab '按ESC键无法响应,改为Tab键,取消输入,恢复原值
- ActiveCell = Original
- HideCtrl
-
- Case vbKeyUp, vbKeyDown '上、下方向键
- If Shift = 2 Then 'Ctrl+上、下方向键,跳到当前活动单元格的上、下方单元格
- With ActiveCell
- Do
- i = i + KeyCode - 39 '↑、↓键码为38、40,KeyCode - 39 = ±1
- lRow = .Row + i
- If lRow < 1 Or lRow > Rows.Count Then Exit Do '超出工作表行数范围
- If Rows(lRow).Height Then .Offset(i).Activate: Exit Do '隐藏行高为0
- Loop
- End With
- ElseIf .ListCount > 2 Then 'Listbox中有数据行时
- ListIdx = ListIdx + KeyCode - 39
- If ListIdx <= 0 Then ListIdx = .ListCount - 1 '选中第一行标题时变成选最后一行
- If ListIdx >= .ListCount Then ListIdx = 1 '超过最后一行后返回到第二行(第一行为标题)
- .ListIndex = ListIdx
- End If
-
- Case vbKeyLeft, vbKeyRight '左、右方向键
- If Shift = 2 Then 'ActiveCell.Offset(, KeyCode - 38).Activate
- With ActiveCell
- Do
- i = i + KeyCode - 38 '←、→键码为37、39,KeyCode - 38 = ±1
- lCol = .Column + i
- If lCol < 1 Or lCol > Columns.Count Then Exit Do '超出工作表列数范围
- If Columns(lCol).Width Then .Offset(, i).Activate: Exit Do '隐藏列宽为0
- Loop
- End With
- End If
- Case Else
- End Select
- End With
- End Sub
- Private Sub ListBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
- Ctrl_KeyUp KeyCode, Shift
- End Sub
- Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
- Ctrl_KeyUp KeyCode, Shift
- End Sub
- Private Sub Worksheet_SelectionChange(ByVal Target As Range)
- '判断是否符合条件
- 'If InputSwitchFlag = False Then Exit Sub '不能用HideCtrl,事件中运行任何代码修改值或属性都会影响复制后的粘贴功能,
- '这句代码这样设置,InputSwitchFlag = False时不影响粘贴。如果用Application.EnableEvents = False会影响其他事件使用。
- '使用Application.CutCopyMode<>False也有问题,单元格编辑状态(双击)下复制无法探测。检测剪贴板上是否有内容也不完美。
- If Intersect(Target, Range(RangeAddr)) Is Nothing Then HideCtrl: Exit Sub
- If Target.Count > 1 Then HideCtrl: Exit Sub
- If IsEmpty(arr) Then Call GetData
- If IsEmpty(arr) Then HideCtrl: Exit Sub
-
- '初始化准备工作
- '设定控件的尺寸、位置、字体等 属性
- ListBox1.Visible = False
- TextBox1.Visible = False
- With TextBox1
- .Top = Target.Top
- .Left = Target.Left
- .Width = Target.Width
- .Height = Target.Height
- .Font.Size = Target.Font.Size - 1
- .BorderStyle = fmBorderStyleSingle
- .BorderColor = &H80000006
- .Text = ActiveCell.Value
- Original = .Text
- .Activate
- .Visible = True
- End With
- With ListBox1
- .Top = Target.Top + Target.Height + 2
- .Left = Target.Left + Target.Width
- .Height = 200 '高度
- .Width = 600 '宽度
- .Font.Size = 10
- .ForeColor = vbBlue
- .BackColor = 15849925
- .ColumnCount = UBound(arr, 2) ' '列表框的列数=数组的列数
- .ColumnWidths = "80;80;80;50;50;50;50;50;50;50;50;50"
- .Visible = True
- End With
- TextBox1_Change
- End Sub
- Private Sub WriteInto() '填入内容到哦工作表
- Dim brr
- With ListBox1
- If .ListCount < 2 Then '没有查询到数据,直接输入TextBox1内容
- ActiveCell = TextBox1.Text
- Else
- If .ListIndex = 0 Then Exit Sub
- ReDim brr(1 To .ColumnCount)
- For i = 2 To UBound(brr)
- brr(i) = .List(.ListIndex, i - 1)
- Next
- ActiveCell.Resize(1, UBound(brr)) = brr
- End If
- TextBox1.Text = ""
- 'ActiveCell.Offset(1).Activate
-
- 'SmallScroll可有四个参数:
- 'Down 将内容向下滚动的行数。
- 'Up 将内容向上滚动的行数。
- 'ToRight 将内容向右滚动的列数。
- 'ToLeft 将内容向左滚动的列数。
- 'ActiveWindow.SmallScroll Down:=1
- End With
- End Sub
- Private Sub TextBox1_Change()
- Dim s$, t$, i&, j&, u&, brr
- If IsEmpty(arr) Then Exit Sub
- t = UCase(TextBox1)
- With ListBox1
- If Len(t) = 0 Then
- .List = arr
- .ListIndex = 1
- Exit Sub
- End If
-
- ReDim brr(1 To UBound(arr, 2), 1 To 1) '定义二维数组brr,1到数组的列数
- For i = 2 To UBound(arr, 2) '标题 ''UBound(brr, 2)数组的列数表达
- brr(i, 1) = arr(2, i)
- Next
- .Clear
- For i = 2 To UBound(arr) '数组的行数的表达,
- s = arr(i, 1) & "|" & arr(i, 2) & "|" & arr(i, 3) & "|" & arr(i, 4) & "|" & arr(i, 5) '多条件模糊查询,只需把各列串联起来即可。
- If InStr(s, t) Then
- ReDim Preserve brr(1 To UBound(arr, 2), 1 To UBound(brr, 2) + 1) 'UBound(brr, 2)数组的列数表达
- u = UBound(brr, 2) 'UBound(brr, 2)数组的列数表达
- For j = 1 To UBound(arr, 2) '想显示几列就赋值几列
- brr(j, u) = arr(i, j)
- Next
- End If
- Next
- .Column = brr '用Column属性赋值无需转置数组
- If UBound(brr, 2) > 1 Then .ListIndex = 1
- End With
- End Sub
- Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
- WriteInto
- End Sub
复制代码
@ivccav 请问向图片中的双标题行如果修改呢?有一个是数据源表的总标题,第4行才是数据源的标题行。。 |
-
|