我有我的脚本,它允许我通过输入TextBox来搜索我的数据。问题是我的是静态的(例如,第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发布于 2018-10-11 13:39:12
我更喜欢使用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发布于 2018-10-11 14:07:26
非常感谢TinMan,我刚刚添加了这个If TextBox1 <> "" Then,否则E列在开头已经是绿色的了。
而且,你是对的,那是我的参考http://www.blog-excel.com/creer-un-champ-de-recherche-vba/
祝你今天愉快:)
纪尧姆
https://stackoverflow.com/questions/52761145
复制相似问题