gpt4 book ai didi

excel - VBA 动态搜索模式与动态

转载 作者:行者123 更新时间:2023-12-04 22:31:03 25 4
gpt4 key购买 nike

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

此致

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

最佳答案

我更喜欢使用 For each环形。您还应该始终完全限定您的引用。

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

关于excel - VBA 动态搜索模式与动态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52761145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com