我有一个超过19,000行的列。我要做的是运行一个vba代码,该代码将选择该列中的一系列单元格,并为所选范围内的序列中缺少的每个数字添加一个空行。现在,我正在使用的代码将允许我选择一系列单元格但是在我选择所述范围之后,它将给出行 gap = Right(.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
答案 0 :(得分:1)
更详细地开发我的评论答案,并将您的代码重构为最低要求:
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