用于根据条件移动行的VBA代码

我是新的StackExchange和VBA,我试图find一个答案,但我有问题!

我有一个电子表格,input信息并出现在表2的列BI中。 我想要做的是,如果H列是空白的,它将整行移动到列L到S.所以我的数据将被分为2个单独的地方,在同一张表H是否为空或填充。

H列中的信息每次都会有所不同,所以我不能指定列中的内容,但是有时候什么也没有input到列H中,也就是我希望它移动的时候。

谁能告诉我如何做到这一点? 非常感谢Bev

尝试这个起点

 Option Explicit Const FIRST_ROW As Long = 2 Const LAST_ROW As Long = 100 Const FIRST_SOURCE_COL As Long = 2 'column B Const LAST_SOURCE_COL As Long = 9 'column I Const TRIG_COL As Long = 8 'column H Const GAP As Long = 2 'columns gap between original data and moved data Sub moveit() Dim r As Long Dim c As Long Const DIST As Long = LAST_SOURCE_COL - FIRST_SOURCE_COL + GAP + 1 For r = FIRST_ROW To LAST_ROW 'check if we need to move If Cells(r, TRIG_COL).Value = "" Then 'move the data For c = FIRST_SOURCE_COL To LAST_SOURCE_COL Cells(r, c + DIST).Value = Cells(r, c).Value Cells(r, c).Value = "" Next End If Next End Sub 

移动空白,

 Sub MoveBlanks() Dim lRws As Long, sh As Worksheet, x Set sh = Sheets("Sheet2") With sh lRws = .Cells(.Rows.Count, "B").End(xlUp).Row For x = lRws To 2 Step -1 If .Cells(x, "H") = "" Then .Range(.Cells(x, "B"), .Cells(x, "I")).Cut .Cells(.Rows.Count, "L").End(xlUp).Offset(1) .Range(.Cells(x, "B"), .Cells(x, "I")).Delete Shift:=xlUp End If Next x End With End Sub 

之前

在这里输入图像说明

在这里输入图像说明