VBA代码,它将查找特定条件,以及是否将来自不同列的数据与另一列中的数据匹配

我需要一个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空一行…我也试图剪切/粘贴和偏移,但我得到的是错误消息。

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

我还没有开始,但将不胜感激任何build议在Do-Until循环。

我已经附加了我的工作performance在的样子的图片与我在macros观运行后需要的图片。 另外,移动的行并不总是包含4个单元格,有时候会有更多,这就是为什么我需要这样做,而不是一个设定的范围。

(2) 之前 [1]

尝试这个

 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 

在这里输入图像说明