首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态VBA动态搜索模式

动态VBA动态搜索模式
EN

Stack Overflow用户
提问于 2018-10-11 13:22:12
回答 2查看 101关注 0票数 1

我有我的脚本,它允许我通过输入TextBox来搜索我的数据。问题是我的是静态的(例如,第6行到第30行)。如果我添加了一个新行,它将不会被我的脚本所接受。我想我必须使用变量,而不是预定义的范围,但我不知道怎么做。

诚挚的问候

代码语言:javascript
复制
Option Compare Text

Private Sub TextBox1_Change()

    Application.ScreenUpdating = False

    Range("E8:E30").Interior.ColorIndex = 24

    If TextBox1 <> "" Then
        For ligne = 8 To 30
            If Cells(ligne, 5) Like "*" & TextBox1 & "*" Then
                Cells(ligne, 5).Interior.ColorIndex = 43
            End If
        Next
    End If
End Sub
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-11 13:39:12

我更喜欢使用For each循环。你也应该始终充分限定你的参考资料。

代码语言:javascript
复制
Private Sub TextBox1_Change()
    Application.ScreenUpdating = False
    Dim cell As Range, Target As Range
    With Worksheets("Sheet1")
        Set Target = .Range("E8", .Range("E" & .Rows.Count).End(xlUp))
        Target.Interior.ColorIndex = 24

        For Each cell In Target
            If cell.Value Like "*" & TextBox1 & "*" Then cell.Interior.ColorIndex = 43
        Next

    End With
    Application.ScreenUpdating = True
End Sub
票数 2
EN

Stack Overflow用户

发布于 2018-10-11 14:07:26

非常感谢TinMan,我刚刚添加了这个If TextBox1 <> "" Then,否则E列在开头已经是绿色的了。

而且,你是对的,那是我的参考http://www.blog-excel.com/creer-un-champ-de-recherche-vba/

祝你今天愉快:)

纪尧姆

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52761145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档