Excel 第二次遇到该值时返回不同的结果
我在VBA中使用了一种搜索函数。我第一次识别一个单词在列中时,我会在它旁边的列中输入一个值。但是第二次这个词出现在列中时,我希望它旁边的列中有另一个值Excel 第二次遇到该值时返回不同的结果,excel,vba,if-statement,Excel,Vba,If Statement,我在VBA中使用了一种搜索函数。我第一次识别一个单词在列中时,我会在它旁边的列中输入一个值。但是第二次这个词出现在列中时,我希望它旁边的列中有另一个值 Sub Kalender() Application.ScreenUpdating = False Dim cl As Range With ActiveSheet For Each cl In Application.Intersect(.Columns("E"), .UsedRang
Sub Kalender()
Application.ScreenUpdating = False
Dim cl As Range
With ActiveSheet
For Each cl In Application.Intersect(.Columns("E"), .UsedRange)
If cl.Value = "Agent" Then
cl.Offset(, -3).Value = "MONDAY"
End If
Next cl
Application.ScreenUpdating = True
End With
End Sub
现在我有这个,但我需要第二个值,因为第二次列E包含“Agent”。您可以引入一个变量,如
found\u once
,当您第一次找到值“Agent”时,它将变为True
Sub Kalender()
Application.ScreenUpdating = False
Dim cl As Range
Dim found_once As Boolean
With ActiveSheet
For Each cl In Application.Intersect(.Columns("E"), .UsedRange)
If cl.Value = "Agent" Then
If found_once Then
cl.Offset(, -3).Value = "MONDAY_again"
Else
cl.Offset(, -3).Value = "MONDAY"
found_once = True
End If
End If
Next cl
End With
Application.ScreenUpdating = True
End Sub
如果需要保留发生次数,可以使用变量,如found\u cases
:
Sub Kalender2()
Application.ScreenUpdating = False
Dim cl As Range
Dim found_cases As Byte
With ActiveSheet
For Each cl In Application.Intersect(.Columns("E"), .UsedRange)
If cl.Value = "Agent" Then
cl.Offset(, -3).Value = "MONDAY " & found_cases + 1
found_cases = found_cases + 1
End If
Next cl
End With
Application.ScreenUpdating = True
End Sub
第二次输入的值是多少?还会有第三次吗/