select一个单元格的范围,并插入一个空行的每个数字,这些单元格的序列是closures的

我有超过19,000行的专栏。 我正在做的是运行一个vba代码,将select该列中的单元格范围,并为选定范围内的序列中缺less的每个数字添加一个空行。 现在,我正在使用的代码将允许我select一个范围的单元格,但是,当我select所述的范围后,它会给我一个types不匹配错误的行间隙=右(.Cells(i),5) – 右.Cells(i – 1),5) 。 如果我把这些单元格的范围复制到一个新的表格中,那么这个代码就是我想要的。 任何想法,为什么当我在超过19000个单元格的列上运行时,它会给我一个不匹配的错误?

我正在使用的代码是:

Option Explicit Sub InsertNullBetween() Dim i As Long, gap As Long 'Update 20130829 Dim WorkRng As Range Dim Rng As Range Dim outArr As Variant Dim dic As Variant Set dic = CreateObject("Scripting.Dictionary") 'On Error Resume Next Set WorkRng = Application.Selection Set WorkRng = Application.InputBox("Range", WorkRng.Address, Type:=8) With Range("A1", Cells(Rows.Count, 1).End(xlUp)) For i = .Rows.Count To 2 Step -1 gap = Right(.Cells(i), 5) - Right(.Cells(i - 1), 5) If gap > 1 Then .Cells(i).Resize(gap - 1).Insert xlDown Next End With End Sub 

为了更详细地开发我的回答,并将你的代码重构一下,

 Option Explicit Sub InsertNullBetween() Dim i As Long, gap As Long Dim WorkRng As Range On Error Resume Next Set WorkRng = Application.InputBox(Prompt:="Range To Check", Title:="Select a Range", Default:=Selection.address, Type:=8) On Error GoTo 0 If WorkRng Is Nothing Then Exit Sub '<--| check user hasn't canceled the dialog box With WorkRng For i = .Rows.count To 2 Step -1 gap = Right(.Cells(i), 5) - Right(.Cells(i - 1), 5) If gap > 1 Then .Cells(i).Resize(gap - 1).Insert xlDown Next End With End Sub