[英]Excel VBA- copy and insert row based on cell value
我正在努力做到这一点:
G列========>新G列
2 1
2
2 1
2
1 1
1 1
2 1
2
我已经看过许多不同的问题来回答这个问题,但是我认为我的代码是错误的,因为我想在最初G = 2时复制整行并将其直接插入下面,而不是通常将其复制到excel中的另一张纸上。
Sub duplicate()
Dim LastRow As Long
Dim i As Integer
For i = 2 To LastRow
If Range("G" & i).Value = "2" Then
Range(Cells(Target.Row, "G"), Cells(Target.Row, "G")).Copy
Range(Cells(Target.Row, "G"), Cells(Target.Row, "G")).EntireRow.Insert Shift:=xlDown
End If
Next i
End Sub
非常感谢您提供的所有帮助!!
Public Sub ExpandRecords()
Dim i As Long, s As String
Const COL = "G"
For i = 1 To Cells(Rows.Count, COL).End(xlUp).Row
If Cells(i, COL) = 2 Then
s = s & "," & Cells(i, COL).Address
End If
Next
If Len(s) Then
Range(Mid$(s, 2)).EntireRow.Insert
For i = 1 To Cells(Rows.Count, COL).End(xlUp).Row
If Cells(i, COL) = vbNullString Then
Cells(i, COL).Value = 1
End If
Next
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.