我有一列有超過19,000行的列。我期望做的是運行一個vba代碼,該代碼將選擇該列中的一系列單元格,併爲所選範圍內的序列中缺少的每個數字添加一個空行。現在我正在使用的代碼將允許我選擇一個範圍的單元格,但是在選擇了所述範圍之後,它會給我一個類型不匹配錯誤,對於行gap = Right(.Cells(i),5) - Right (.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
嘗試替換'使用範圍(「A1」,Cells(Rows.count,1).End(xlUp))'With With WorkRng' – user3598756