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

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

Insert blank row based on cell value - 1

问题

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

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

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

  1. Sub Main()
  2. '---变量---
  3. Dim source As Worksheet
  4. Dim startRow As Integer
  5. Dim num As Integer
  6. Dim val As String
  7. Dim i As Long
  8. '---自定义---
  9. Set source = ThisWorkbook.Sheets(1) '包含数据的工作表
  10. startRow = 2 '包含数据的第一行
  11. '---逻辑---
  12. i = startRow 'i作为行计数器
  13. Do While i <= source.Range("O" & source.Rows.Count).End(xlUp).Row
  14. '循环直到我们到达列E中的最后一个带值的行
  15. num = source.Range("R" & i).Value '获取出现次数
  16. val = source.Range("A" & i).Value '获取值
  17. If num > 1 Then '出现次数大于1
  18. Do While num > 1 '创建行
  19. source.Range("A" & i + 1).EntireRow.Insert '插入行
  20. num = num - 1
  21. i = i + 1 '下一行
  22. Loop
  23. End If
  24. i = i + 1 '下一行
  25. Loop
  26. 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:

  1. `Sub Main()
  2. &#39;---Variables---
  3. Dim source As Worksheet
  4. Dim startRow As Integer
  5. Dim num As Integer
  6. Dim val As String
  7. Dim i As Long
  8. &#39;---Customize---
  9. Set source = ThisWorkbook.Sheets(1) &#39;The sheet with the data
  10. startRow = 2 &#39;The first row containing data
  11. &#39;---Logic---
  12. i = startRow &#39;i acts as a row counter
  13. Do While i &lt;= source.Range(&quot;O&quot; &amp; source.Rows.Count).End(xlUp).Row
  14. &#39;looping until we hit the last row with a value in column E
  15. num = source.Range(&quot;R&quot; &amp; i).Value &#39;Get number of appearances
  16. val = source.Range(&quot;A&quot; &amp; i).Value &#39;Get the value
  17. If num &gt; 1 Then &#39;Number of appearances &gt; 1
  18. Do While num &gt; 1 &#39;Create rows
  19. source.Range(&quot;A&quot; &amp; i + 1).EntireRow.Insert &#39;Insert row
  20. num = num - 1
  21. i = i + 1 &#39;Next row
  22. Loop
  23. End If
  24. i = i + 1 &#39;Next row
  25. Loop
  26. End Sub`
  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. 在插入行时,从底部到顶部更容易。例如:
  5. ```lang-vb
  6. Sub Main()
  7. Dim source As Worksheet, num As Long, c As Range
  8. With ThisWorkbook.Sheets(1) '数据所在的工作表
  9. '从ColO中的最后一个占用行的ColR开始
  10. Set c = .Cells(.Cells(.Rows.Count, "O").End(xlUp).Row, "R")
  11. Debug.Print "从 " & c.Address & " 开始在 '" & .Name & "' 上"
  12. End With
  13. Do While c.Row > 1
  14. num = c.Value '获取出现次数
  15. If num > 1 Then
  16. c.Offset(1).Resize(num - 1).EntireRow.Insert shift:=xlShiftDown
  17. End If
  18. Set c = c.Offset(-1)
  19. Loop
  20. End Sub

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

英文:

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

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

答案2

得分: 0

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

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

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

  1. 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:

确定