Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据其他列中的值在excel中插入行_Excel_Ms Access_Vba - Fatal编程技术网

根据其他列中的值在excel中插入行

根据其他列中的值在excel中插入行,excel,ms-access,vba,Excel,Ms Access,Vba,这是新来的,所以请容忍我 我想根据另一列中的值自动插入某个数字或行。这能做到吗 乙二醇 因此,容器将按提升次数进行拆分,一个用于嵌套在while循环中的循环应该可以完成您的工作。为什么我们起诉while而不是for,因为在现有数据之间添加新行时,我们无法动态更改for循环的限制。那你就来了: Dim LastRow As Long Dim RowAddNo As Long LastRow = Range("A1").End(xlDown).Row i = 2 While i <= Last

这是新来的,所以请容忍我

我想根据另一列中的值自动插入某个数字或行。这能做到吗

乙二醇


因此,容器将按提升次数进行拆分,一个用于嵌套在while循环中的循环应该可以完成您的工作。为什么我们起诉while而不是for,因为在现有数据之间添加新行时,我们无法动态更改for循环的限制。那你就来了:

Dim LastRow As Long
Dim RowAddNo As Long
LastRow = Range("A1").End(xlDown).Row
i = 2
While i <= LastRow
    RowAddNo = Range("B" & i).Value
    For J = 1 To RowAddNo
        Rows(i + 1 & ":" & i + 1).Insert shift:=xlUp
        LastRow = LastRow + 1
        i = i + 1 'increasing the 'i' value to find the address for the next not empty row
    Next
    i = i + 1 'increasing the 'i' value to find the address for the next not empty row
Wend
Dim LastRow尽可能长
暗淡的路没有那么长
LastRow=范围(“A1”)。结束(xlDown)。行
i=2

而我这会帮你做到:

Sub AddRows()
Dim X As Long
For X = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1 'Work backwards when inserting or deleting rows, so much easier than incrementing numbers
    Range("A" & X).Offset(1, 0).Resize(Range("B" & X).Value, 1).EntireRow.Insert 'Insert the number of rows against the target row offset by 1 ie below it
Next
End Sub

行号不再总是适合整数,请使用Long
Sub AddRows()
Dim X As Long
For X = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1 'Work backwards when inserting or deleting rows, so much easier than incrementing numbers
    Range("A" & X).Offset(1, 0).Resize(Range("B" & X).Value, 1).EntireRow.Insert 'Insert the number of rows against the target row offset by 1 ie below it
Next
End Sub