我有一个Excel表,该表带有一个有序的文本列和一个MoveToRow列。该表的顺序正确,但是我需要使用MoveToRow列在值之间添加空行来隔开行。
例如:
1. cat 2
2. dog 3
3. Rat 5
Excel中是否有一种方法可以使用公式或vb或c#代码将动物字段移动或复制到第二列中列出的所需行?
结果:
1.
2.cat
3.dog
4.
5.rat
或者如果可能的话,我可以将MoveToRow列更改为InsertThisManyRowsAfter列。
我会认为类似这样的代码会循环遍历整数列,该列包含要在当前列下方插入的行数;如果值是0,则跳过它;但是如果值> = 1,它将插入该行数。但这不能正常工作,因为它添加了太多行,并且所有行都在同一范围内。这里的逻辑哪里不正确?
Sub Test1()
Dim x As Integer
Dim y As Integer
Dim z As Integer
' Set numrows = number of rows of data.
' NumRows = Range("A2", Range("A2").End(xldown)).Rows.Count
NumRows = 100
' Select cell a1.
Range("I1").Select
' Establish "For" loop to loop "numrows" number of times.
For x = 1 To NumRows
' Insert your code here.
' Selects cell down 1 row from active cell.
y = ActiveCell.Row
z = ActiveCell.Value
If z >= 1 Then
Rows(y & ":" & y + z).Insert Shift:=xlShiftDown
ActiveCell.Offset(1, 0).Select
Else
ActiveCell.Offset(1, 0).Select
End If
Next
结束子
答案 0 :(得分:0)
考虑到您发布的示例,这对我来说很好。
Public Sub ReverseLoop()
Dim sht As Worksheet
Dim LastRow As Long
Dim r As String
Dim x As Long
Dim NumList As Variant 'Array
Dim RegExpObj As Object
Dim NumStr As Integer
Set sht = ActiveSheet
'Ctrl + Shift + End
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
'Loop Through 0-100 in Reverse Order
For x = LastRow To 1 Step -1
strIn = Range("A" & x).Value
Set RegExpObj = CreateObject("vbscript.regexp")
With RegExpObj
.Global = True
.Pattern = "[^\d]+"
NumStr = .Replace(strIn, " ")
End With
Cells(x, 1).EntireRow.Select
Selection.Cut
Cells(NumStr, 1).Select
ActiveSheet.Paste
Next x
End Sub