在 A 列中,我有一些数字,由于前导零而被格式化为文本:
+------+
| A |
+------+
| 0001 |
| 0002 |
| 0003 |
| 0012 |
| 0013 |
| 0015 |
| 0017 |
| 0019 |
| 0020 |
+------+
使用 A 列作为输入,我想填充另外两列,B 和 C,如下所示:
+------+------+
| B | C |
+------+------+
| 0001 | 0003 |
| 0012 | 0013 |
| 0015 | |
| 0017 | |
| 0019 | 0020 |
+------+------+
我基本上想要每个数字范围的开始和结束。
应从 B 列和 C 列的范围中省略 A 列中未包含的数字。
如何在 Excel 中实现这一点?
答案1
这应该可以做到:
Sub dural()
Dim N As Long, i As Long, K As Long, v As Long
Dim vOld As Long
N = Cells(Rows.Count, "A").End(xlUp).Row + 1
K = 1
For i = 1 To N
v = CLng(Cells(i, 1).Value)
If i = 1 Then
Cells(1, "B").Value = v
vOld = v
Else
If v = vOld + 1 Then
Else
Cells(K, "C").Value = vOld
K = K + 1
Cells(K, "B") = v
End If
vOld = v
End If
Next i
For i = 1 To K
If Cells(i, "B").Value = Cells(i, "C").Value Then Cells(i, "C").Value = ""
If Cells(i, "B").Value = 0 Then Cells(i, "B").Value = ""
Next i
End Sub