插入空行,基于单元格的值 – 1

huangapple go评论63阅读模式
英文:

Insert blank row based on cell value - 1

问题

在列R中有数字(有时一张表上有4行,有时超过1000行),需要在任何列R中的值大于1的行下面添加空白行。

第2行列R的值为1,因此不需要添加空行。第3行列R的值为3,因此需要在其下添加2个空白行。第4行列R的值为2,因此需要在其下添加1个空白行,依此类推。

我在这里找到了这段代码,它曾经有效,但现在不再有效:

Sub Main()

'---变量---
Dim source As Worksheet
Dim startRow As Integer
Dim num As Integer
Dim val As String
Dim i As Long

'---自定义---
Set source = ThisWorkbook.Sheets(1) '包含数据的工作表
startRow = 2 '包含数据的第一行

'---逻辑---
i = startRow 'i作为行计数器
Do While i <= source.Range("O" & source.Rows.Count).End(xlUp).Row
'循环直到我们到达列E中的最后一个带值的行
    num = source.Range("R" & i).Value '获取出现次数
    val = source.Range("A" & i).Value '获取值
    If num > 1 Then '出现次数大于1
        Do While num > 1 '创建行
            source.Range("A" & i + 1).EntireRow.Insert '插入行
            num = num - 1
            i = i + 1 '下一行
        Loop
    End If
    i = i + 1 '下一行
Loop

End Sub

希望这对你有帮助。

英文:

I have numbers in col R (sometimes there are 4 lines on a sheet, other times over 1000), and need to add blank rows beneath any row that has more than the value 1 in column R.

Row 2 has value of 1 in column R so no blanks are needed, Row 3 has value of 3, so I need to add 2 blank rows below it. Row 4 has value of 2 so I need to add 1 blank row below, etc.

I found this code on here and it was working but now it is not:

`Sub Main()

&#39;---Variables---
Dim source As Worksheet
Dim startRow As Integer
Dim num As Integer
Dim val As String
Dim i As Long

&#39;---Customize---
Set source = ThisWorkbook.Sheets(1) &#39;The sheet with the data
startRow = 2 &#39;The first row containing data

&#39;---Logic---
i = startRow &#39;i acts as a row counter
Do While i &lt;= source.Range(&quot;O&quot; &amp; source.Rows.Count).End(xlUp).Row
&#39;looping until we hit the last row with a value in column E
    num = source.Range(&quot;R&quot; &amp; i).Value &#39;Get number of appearances
    val = source.Range(&quot;A&quot; &amp; i).Value &#39;Get the value
    If num &gt; 1 Then &#39;Number of appearances &gt; 1
        Do While num &gt; 1 &#39;Create rows
            source.Range(&quot;A&quot; &amp; i + 1).EntireRow.Insert &#39;Insert row
            num = num - 1
            i = i + 1 &#39;Next row
        Loop
    End If
    i = i + 1 &#39;Next row
Loop

End Sub`

</details>


# 答案1
**得分**: 1

在插入行时,从底部到顶部更容易。例如:

```lang-vb
Sub Main()
    
    Dim source As Worksheet, num As Long, c As Range
    
    With ThisWorkbook.Sheets(1) '数据所在的工作表
        '从ColO中的最后一个占用行的ColR开始
        Set c = .Cells(.Cells(.Rows.Count, "O").End(xlUp).Row, "R")
        Debug.Print "从 " & c.Address & " 开始在 '" & .Name & "' 上"
    End With
    
    Do While c.Row > 1
        num = c.Value '获取出现次数
        If num > 1 Then
            c.Offset(1).Resize(num - 1).EntireRow.Insert shift:=xlShiftDown
        End If
        Set c = c.Offset(-1)
    Loop
End Sub

请注意,上面的代码已经被翻译成中文。如果您需要进一步的帮助或有其他翻译需求,请随时告诉我。

英文:

When inserting rows it's easier to work from the bottom to the top. For example:

Sub Main()
    
    Dim source As Worksheet, num As Long, c As Range
    
    With ThisWorkbook.Sheets(1) &#39;The sheet with the data
        &#39;start in ColR, at last-occupied row in ColO
        Set c = .Cells(.Cells(.Rows.Count, &quot;O&quot;).End(xlUp).Row, &quot;R&quot;)
        Debug.Print &quot;Starting at &quot; &amp; c.Address &amp; &quot; on &#39;&quot; &amp; .Name &amp; &quot;&#39;&quot;
    End With
    
    Do While c.Row &gt; 1
        num = c.Value &#39;Get number of appearances
        If num &gt; 1 Then
            c.Offset(1).Resize(num - 1).EntireRow.Insert shift:=xlShiftDown
        End If
        Set c = c.Offset(-1)
    Loop
End Sub

答案2

得分: 0

你可能需要将语句中的 O 改为 R

在这段代码中,它确定了循环中将要处理的最后一行。如果在列 `O` 中没有数据,那么什么都不会处理。
英文:

Probably you need to change O to R in the statement:

Do While i &lt;= source.Range(&quot;O&quot; &amp; source.Rows.Count).End(xlUp).Row

This code determines last row that will be processed in the loop. If there is no data in the column O - nothing is processed.

huangapple
  • 本文由 发表于 2023年6月22日 00:54:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定