我希望当 D 列中的数字从 7 位数字变为 11 位数字时添加两行,要么在较低数字下方,要么在较高数字上方。
示例:
1656340
1656352
1656389
3156603873
3156690671
3156738131
D 列按数字顺序排列。
接下来当数字从 11 位跳到带有两个连字符的 17 位时。
示例:
3165694674
3165694674
3168190042
026-1924781-2157120
026-6908033-4106726
028-8563479-8783525
最终结果应该在这些数字长度之间有两行间隙。
其中两组数字是随机生成的,因此值不会总是上升。
用于其他目的的偏移代码,我无法为此工作:
Sub AddingBreakRows()
Dim R As Range
Set R = Range("I2:I400")
Dim FR As Integer
Dim LR As Integer
FR = 1 ' First Row in R
LR = R.Rows.Count ' Last Row in R
Dim Index As Integer
For Index = LR To FR Step -1
If Not IsEmpty(R(Index)) Then
R(Index).Offset(1, 0).EntireRow.Insert
End If
Next
End Sub
试试这个:
Sub AddingBreakRows()
Const V7DIGITS = "#######" '7 digit pattern
Const V11DIGITS = "###########" '11 digits
Const V17DIGITS = "###-#######-#######" 'digits with dashes
Dim rng As Range, ws As Worksheet, arr, r As Long, vU, vL, ins As Boolean
Set ws = ThisWorkbook.Worksheets("Data")
Set rng = ws.Range("I2:I" & ws.Cells(rows.count, "I").End(xlUp).row)
arr = rng.Value 'get all values as array (faster)
'loop over the array of values from the bottom up
For r = UBound(arr, 1) - 1 To 1 Step -1
vU = arr(r, 1) 'upper value
vL = arr(r + 1, 1) 'lower value
'Does the pattern change here, in one of the ways we're
' looking for?
If (vU Like V7DIGITS And vL Like V11DIGITS) Or _
(vU Like V11DIGITS And vL Like V17DIGITS) Then
rng.Cells(r + 1).Resize(2).EntireRow.Insert
End If
Next r
End Sub