Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 第二次遇到该值时返回不同的结果_Excel_Vba_If Statement - Fatal编程技术网

Excel 第二次遇到该值时返回不同的结果

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

我在VBA中使用了一种搜索函数。我第一次识别一个单词在列中时,我会在它旁边的列中输入一个值。但是第二次这个词出现在列中时,我希望它旁边的列中有另一个值

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

第二次输入的值是多少?还会有第三次吗/