Excel VBA查找匹配并返回交替值

时间:2017-09-24 03:21:44

标签: excel excel-vba vba

我无法尝试将某些内容包含在我正在构建的宏中。我需要它来搜索C列 对于那些说"开始反式"并且在(d)中的一列中 - 第一个值将等于零,下一个实例应该是100,下一个实例0接下来是实例100,直到数据结束。

实例并不总是每隔4行,我还有其他零,我希望它忽略。

感谢您的帮助!

Example sheet

2 个答案:

答案 0 :(得分:0)

试试这个。

Sub test()
    Dim rngDB As Range, rng As Range
    Dim n As Long, Result As Integer
    Set rngDB = Range("c5", Range("c" & Rows.Count).End(xlUp))
    For Each rng In rngDB
        If rng = "start trans" Then
            n = n + 1
            If n Mod 2 Then
                Result = 0
            Else
                Result = 100
            End If
            rng.Offset(0, 1) = Result
        End If
    Next rng
End Sub

答案 1 :(得分:0)

这个怎么样:

Sub GoGoGo()
Dim l As Long: Dim i As Long
Dim b As Boolean
With ActiveSheet
    l = .Cells(.Rows.Count, "C").End(xlUp).Row
    For i = 5 To l
        If .Cells(i, "C").Value2 = "start trans" Then .Cells(i, "D").Value2 = b * -100: b = Not b
    Next i
End With
End Sub