ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 126|回复: 1

[求助] 代码偶尔报错求高手指点

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-1-8 21:12 | 显示全部楼层 |阅读模式
  1. Private Sub TextBox1_Change()
  2. If x = 1 Then  '
  3. ActiveCell = TextBox1.Value
  4. Me.ListBox1.Clear
  5. On Error Resume Next
  6. <font color="#ff0000">    For i = LBound(frr) To UBound(frr)</font> '<font color="#ff0000">会出现错误这段会偶尔报错下标越界</font>
  7.         If InStr(frr(i), Me.TextBox1.Value) > 0 Then
  8.          Me.ListBox1.AddItem frr(i)
  9.     End If
  10.     Next
  11.     If TextBox1 <> "" And ListBox1.ListCount > 0 Then ListBox1.ListIndex = 0
  12. End If
  13. '---------------------------------------------------------------------------
  14. If x = 2 Then '工程名称
  15. ActiveCell = TextBox1.Value
  16. Me.ListBox1.Clear
  17.     For i = LBound(frr) To UBound(frr)
  18.         If InStr(frr(i), Me.TextBox1.Value) > 0 Then
  19.          Me.ListBox1.AddItem frr(i)
  20.     End If
  21.     Next
  22.     If TextBox1 <> "" And ListBox1.ListCount > 0 Then ListBox1.ListIndex = 0
  23.       
  24. End If

  25. End Sub

  26. Private Sub TextBox1_GotFocus()
  27. Me.ListBox1.Clear
  28. If x = 1 Then '公司名称
  29.   If IsEmpty(drr) Then
  30.         With Worksheets("基础信息表")
  31.             drr = .Range("a2:h" & .Range("A65536").End(xlUp).Row)
  32.         End With
  33.     End If
  34.     Dim dic
  35.     Set dic = CreateObject("scripting.dictionary")  '建立字典
  36.     For i = LBound(drr) To UBound(drr)
  37.             dic(drr(i, 2)) = 1
  38.     Next
  39.         Me.ListBox1.List = dic.keys  '获得对应规格型号
  40.         frr = dic.keys
  41. End If
  42.   If x = 2 Then '工程名称
  43.     Me.ListBox1.Clear
  44.         If IsEmpty(drr) Then
  45.             With Worksheets("基础信息表")
  46.                 drr = .Range("a2:h" & .Range("A65536").End(xlUp).Row)
  47.             End With
  48.         End If
  49.         Dim gd As String
  50.         gd = ActiveCell.Offset(-1, 0).Value
  51.         If gd = "" Then Exit Sub

  52.         Dim dic2
  53.         Set dic2 = CreateObject("scripting.dictionary")  '建立字典
  54.         For i = LBound(drr) To UBound(drr)
  55.             If drr(i, 2) = gd Then
  56.                 dic2(drr(i, 3)) = 1
  57.             End If
  58.         Next i
  59.         grr = dic2.keys  '
  60.         Me.ListBox1.List = dic2.keys
  61.         frr = dic2.keys
  62.         Dim dic1
  63.         Set dic1 = CreateObject("scripting.dictionary")  '建立字典
  64.         For i = LBound(grr) To UBound(grr)
  65.             If InStr(grr(i), Me.TextBox1.Value) > 0 Then
  66.                 dic1(grr(i)) = 1
  67.             End If
  68.         Next i
  69.         Me.ListBox1.List = dic1.keys  '获得对应规格型号
  70. End If
  71. End Sub

  72. Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  73. Dim i As Integer
  74. Select Case KeyCode
  75.     Case vbKeyDown
  76.         i = ListBox1.ListIndex + 1
  77.         If i < ListBox1.ListCount Then ListBox1.ListIndex = i Else ListBox1.ListIndex = 0
  78.     Case vbKeyUp
  79.         i = ListBox1.ListIndex - 1
  80.         If i > -1 Then ListBox1.ListIndex = i Else ListBox1.ListIndex = ListBox1.ListCount - 1
  81.     Case vbKeyReturn
  82.         If ListBox1.ListIndex = -1 Then
  83.             ActiveCell.Offset(1, 0).Activate
  84.         Else
  85.             ActiveCell = ListBox1.List(ListBox1.ListIndex)
  86.             ActiveCell.Offset(0, 1).Activate
  87.         End If
  88. End Select
  89. End Sub
  90. Private Sub Worksheet_SelectionChange(ByVal Target As Range)  '鼠标选定触发
  91. drr = Sheet1.Range("a2:h" & Sheet1.Range("A65536").End(xlUp).Row)
  92. If Target.Count > 1 Then Exit Sub
  93. With TextBox1
  94. If (Target.Column = 2 And Target.Row = 1) Or (Target.Column = 2 And Target.Row = 2) Then   '第 列第 行
  95. x = Target.Row
  96. .Top = Target.Top
  97. .Width = Target.Width
  98. .Left = Target.Left
  99. .Height = Target.Height
  100. .Value = Target.Value
  101. .Visible = True
  102. .Activate
  103. With ListBox1
  104. .Top = TextBox1.Top + TextBox1.Height
  105. .Left = TextBox1.Left
  106. .Width = TextBox1.Width
  107. .Visible = True
  108. .ListIndex = -1
  109. End With
  110. Else
  111. .Visible = False
  112. ListBox1.Visible = False
  113. End If
  114. End With
  115. End Sub
复制代码
此段代码为了实现二级模糊匹配输入公司名称和对应的工程名称,红色部分偶尔会出现下标越界的错误提示,猜测是因为frr没有取到值,尝试使用 IsEmpty(frr)判断为空从新获取数据,发现frr为空时也不认定为空,求高手解惑,谢谢!

TA的精华主题

TA的得分主题

发表于 2024-1-9 01:02 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
image.png

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-6-16 12:01 , Processed in 0.032831 second(s), 14 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表