2014-12-09 107 views
0

我在寫一個VBA問題,它發現被認爲是無效的行,將行號保存在一個名爲invalidRowsToDelete的數組中,並通過構建一個刪除選定的行無效行的字符串(例如1:1, 4:4, 7:7)。選擇一個範圍的單元格拋出運行時錯誤'1004'

但是,它有時會起作用,有時不起作用,但會對大量無效行失敗。

返回的錯誤是:「運行時錯誤‘1004’:對象的‘_global’失敗的方法範圍」上線Set rng = Range(invalidRowsToDelete)

Public rng As Range 
__________________________ 


Dim invalidRowsToDelete As String 
Dim i As Long 

For i = LBound(InvalidFilesArr) To UBound(InvalidFilesArr) 

    If InvalidFilesArr(i) <> "" Then 
     invalidRowsToDelete = invalidRowsToDelete & InvalidFilesArr(i) & ":" & InvalidFilesArr(i) &  "," 
    Else 
     Exit For 
    End If 
Next i 

'Build range statement and delete trailing comma from rowsToDeleteStatement 
invalidRowsToDelete = "" & Left(invalidRowsToDelete, Len(invalidRowsToDelete) - 1) & "" 
Debug.Print invalidRowsToDelete 

Worksheets("Sheet1").Activate 

Set rng = Range(invalidRowsToDelete) #### Problem line 

rng.Select 
rng.Delete 

這讓我相當長一段時間,現在我無法找出導致此錯誤的原因。

謝謝

+1

我的猜測,雖然我找不到任何支持它,但是你的字符串變得太長了,並且溢出了'range'方法。 – Degustaf 2014-12-09 13:50:14

+0

極限似乎是255. – 2014-12-09 14:14:04

回答

2

使用字符串構建複雜的範圍引用通常是一個壞主意。我將使用循環來構建範圍,而不是描述範圍的字符串。

Public rng As Range 
__________________________ 


Dim invalidRowsToDelete As String 
Dim i As Long 

i = LBound(InvalidFilesArr) 
Set rng = Worksheets("Sheet1").Rows(InvalidFilesArr(i)) 

For i = LBound(InvalidFilesArr) + 1 To UBound(InvalidFilesArr) 
    If InvalidFilesArr(i) = "" Then 
     Exit For 
    End If 
    Set rng = Union(rng, Worksheets("Sheet1").Rows(InvalidFilesArr(i))) 
Next i 

rng.Delete 
0

爲什麼不直接刪除那裏的行然後找到它們呢?

Public rng As Range 

Dim invalidRowsToDelete As String 
Dim i As Long 

Worksheets("Sheet1").Activate 
For i = LBound(InvalidFilesArr) To UBound(InvalidFilesArr) 
    If InvalidFilesArr(i) <> "" Then 
     Set rng = Range(InvalidFilesArr(i) & ":" & InvalidFilesArr(i)) 
     rng.Select 
     rng.Delete 
    Else 
     Exit For 
    End If 
Next i 
+2

你需要小心這樣做。刪除第5行後,什麼是第10行現在是第9行。如您所寫,您將刪除錯誤的行。 – Degustaf 2014-12-09 14:01:16

+0

如果將'For I = LBound(InvalidFilesArr)To UBound(InvalidFilesArr)'改爲'For i = UBound(InvalidFilesArr)To LBound(InvalidFilesArr)Step -1',這將工作得很好。 – TheEngineer 2014-12-24 12:52:59

0

您的字符串是「bad」;找出原因替換:

Set rng = Range(invalidRowsToDelete) 

有:

On Error GoTo GhostBusters 
Set Rng = Range(invalidRowsToDelete) 

,並在子的底部包括:

GhostBusters: 
MsgBox Len(invalidRowsToDelete) 
MsgBox invalidRowsToDelete 
On Error GoTo 0 
0

使用Union獲得要刪除的範圍,然後在此範圍內致電Delete。 HTH

Dim rngToDelete As Range 
Dim rngRow As Range 
Dim rowsToDelete(0 To 5) As Long 
Dim i As Long 

rowsToDelete(0) = 2 
rowsToDelete(1) = 5 
rowsToDelete(2) = 7 
rowsToDelete(3) = 9 
rowsToDelete(4) = 10 
rowsToDelete(5) = 12 

For i = LBound(rowsToDelete) To UBound(rowsToDelete) 
    Set rngRow = ActiveSheet.Rows(rowsToDelete(i)) 
    If rngToDelete Is Nothing Then 
     Set rngToDelete = rngRow 
    Else 
     Set rngToDelete = Application.Union(rngToDelete, rngRow) 
    End If 
Next i 

if Not rngToDelete Is Nothing Then rngToDelete.Delete 
相關問題