VBA 将查找特定条件的代码,如果匹配,则将来自不同列的数据放入另一列

VBA code that will look for certain criteria and if it matches place data from a different column into a another one

我需要有关 VBA 代码的帮助,该代码将查找特定条件,如果匹配,则将来自不同列的数据放入另一列。

如果 C 列显示 "Circum + spa" 并且 D 显示“100”,则 F 行中的值需要移动两列到 H 直到 C 列显示 "Circum + spa" 并且 D 显示“0”(它将保留在 F 列中。) 完成的结果看起来像一条蛇。

我开始这个过程的代码是:

    Dim l As Long
    With ActiveSheet
        l = .Cells(.Rows.Count, "C").End(xlUp).Row
        For i = 1 To l
            If .Cells(i, "C").Value2 = "CIRCUM + SPA" And 
            .Cells(i, "D") = "100" Then
            .Cells(i + 1, "F").Value = .Cells(i + 1, "H").Value
        Next
    End With

但目前它只是使 F 列中的下一行为空...我也尝试了 cut/paste 和一个偏移量,但我得到的只是错误消息。

我也知道使用 +1 不会在最终结果中起作用,因为我需要它来获取所有内容,直到满足其他条件。

我还没有开始,但是如果有任何关于 Do-Until 循环的建议,我将不胜感激。

我附上了我的工作表现在的样子与宏运行后我需要它的样子的图片。此外,移动的行并不总是包含 4 个单元格,有时会有更多,这就是为什么我需要 do until 而不是设定范围。

before[1] after (2)

试试这个

Sub Demo()
    Dim ws As Worksheet
    Dim cel As Range, fCell As Range, lCell As Range
    Dim lastRow As Long
    Dim flag As Boolean

    Set ws = ThisWorkbook.Sheets("Sheet4")      'change Sheet4 to your data sheet
    flag = False

    With ws
        lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row    'last row with data in Column C

        For Each cel In .Range("C2:C" & lastRow)   'loop through each cell in Column C
            If UCase(cel.Value) = "CIRCUM + SPA" Then   'check if Command Name is "CIRCUM + SPA"
                If cel.Offset(, 1).Value = 100 Then     'check if SP is 100
                    Set fCell = cel.Offset(1, 0)        'set first cell to be copied in fCell
                    flag = True
                ElseIf cel.Offset(, 1).Value = 0 Then   'check if SP is 0
                    If flag Then                        'move ahead only if ("CIRCUM + SPA" & 100) already found
                        Set lCell = cel.Offset(-1, 0)   'set last cell to be copied in lCell
                        Set rng = .Range(fCell, lCell).Offset(, 3)  'set range using fCell and lCell
                        rng.Cut rng.Offset(, 2)         'move data from Column F to Column H
                        flag = False
                    End If
                End If
            End If
        Next cel
    End With
End Sub