简体   繁体   English

选择单元格范围将引发运行时错误“ 1004”

[英]Selecting a range of cells throws run-time error '1004'

I'm writing a VBA problem which finds lines which are considered to be invalid, saves the row number in an array called invalidRowsToDelete and deletes the selected rows by building a string of the invalid rows (eg 1:1, 4:4, 7:7 ). 我正在写一个VBA问题,该问题查找被认为无效的行,将行号保存在名为invalidRowsToDelete的数组中,并通过构建无效行的字符串来删除选定的行(例如1:1, 4:4, 7:7 invalidRowsToDelete 1:1, 4:4, 7:7 )。

However, it sometimes works, it sometimes doesn't, but fails more consistently with large amounts of invalid rows. 但是,它有时可以工作,有时不能,但是由于大量无效行而更加一致地失败。

The error it returns is: "Runtime error '1004': Method range of object '_global' failed" on the line Set rng = Range(invalidRowsToDelete) 它返回的错误是: Set rng = Range(invalidRowsToDelete)行上的“运行时错误'1004':对象'_global'的方法范围失败”

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

This has had me for quite a while now and I can't work out the reason for it causing this error. 这已经让我有一段时间了,我无法弄清楚导致此错误的原因。

Thanks 谢谢

Using a string to build a complicated range reference is generally a bad idea. 使用字符串构建复杂的范围引用通常不是一个好主意。 I would use the loop to build the range instead of the string that describes the range. 我将使用循环而不是描述范围的字符串来构建范围。

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

Why not just delete the rows there and then when you find them? 为什么不只删除那里的行,然后找到它们呢?

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  

Your string is "bad"; 您的字符串是“坏的”; to find out why replace: 找出替换原因:

Set rng = Range(invalidRowsToDelete)

with: 与:

On Error GoTo GhostBusters
Set Rng = Range(invalidRowsToDelete)

and at the bottom of the sub include: 并且在子菜单的底部包括:

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

Use Union to get the range to be deleted and then call Delete on this range. 使用“ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM