如何插入一行,如果两个数字之间的差异是>在Excel中超过0?
我有一个简单的Excel表,我想在特定的行后面插入n
行
例如:
如果B
和C
列的差值大于0,我想插入n
行。
而要插入的列数是这两个单元格之间的差异。
但是(如果可能的话)想通过插入单元的数量来增加D
列的数量,但是我不知道使用哪个公式。
我已经在VBA中做了一个小macros,但是我没有任何VBA的经验,我只能做第一行,我不能复制行,只需插入必要的行数!
我的代码:
Sub insertCells() Dim x As Long c = Range("C1").Value Dim id As Long id = Range("A1").Value + 1 Dim y As Long b = Range("B1").Value Dim i As Long i = c - b Do Until i = 0 Rows(id).Insert Shift:=xlToRight i = i - 1 Loop End Sub
第一张照片是macros观之前的细胞
第二个是如果这个macros被执行的话,那么这个单元格会是什么样的
你是这个意思吗?
Sub Main() Application.ScreenUpdating = False lastRow = Range("A" & Rows.Count).End(xlUp).Row ' finding last row used For i = lastRow To 1 Step -1 ' iterating from last row to 1st difference = Range("B" & i) - Range("C" & i) ' calculating the difference If difference < 0 Then ' if difference meets your criteria For j = difference To -1 ' how many rows to insert Rows(i).Copy ' copy the origin Range("A" & i + 1).Insert Shift:=xlDown ' insert the copied row Range("D" & i + 1) = Range("D" & i + 1) + Abs(j) ' increment column D Next End If Next Application.CutCopyMode = False End Sub
之前
后