使用string作为范围时出错(VBA Excel)
我正在尝试使用一个string通过使用范围(MaPlage)来select一些单元格,但是这不起作用。 这是代码和我如何构造string:
Dim MaPlage As String Dim MyRange As Range Dim Line As Integer Dim AdrRD as String Dim First_Cell As Boolean First_Cell = True AdrRD = "A" For Line = 1 To 100 Step 1 If Sheet.Range(AdrRD & Line).Value = "" Then If First_Cell = True Then MaPlage = AdrRD & Line First_Cell = False Else MaPlage = MaPlage & ", " & AdrRD & Line End If End If Next Line If MaPlage <> "" Then Range(MaPlage).Select Selection.EntireRow.Delete End If
“Range(MaPlage)”有错误。请select“
我也试过设置MyRange =范围(MaPlage),但它给了我同样的错误。
Excel给了我法语错误,但它是这样的:“错误1004:从对象'_Global'的'范围'方法失败。
非常感谢!
编辑:我只是试着用
For Line = 1 To 40 Step 1
而不是100,它的工作是正确的。 这是否意味着,当select一直到100线,这是大?
它会出现在Excel中的最大地址长度是255个字符。
然而,你几乎不需要build立一个地址作为一个string。
Dim MyRange As Range Dim c As Range Dim Line As Long For Line = 1 To 100 Set c = Sheet.Cells(Line, "A") If Len(c.Value) = 0 Then If MyRange Is Nothing Then Set MyRange = c Else Set MyRange = Application.Union(MyRange, c) End If End If Next MyRange.EntireRow.Delete
根据工作表UsedRange
的大小和您的需要,您可能会UsedRange
完成工作
Sheet.Range("A1:A100").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
但是如果找不到任何单元格,就会引发一个错误,并且可能会导致SelectionChange
事件无端触发(Excel错误IMO)。