简体   繁体   English

下标超出VBA范围

[英]Subscript out of range on VBA

I am writing my first VBA. 我正在写我的第一个VBA。 I have a sheet named Sheet1 . 我有一个名为Sheet1的工作表。 That sheet has a column E ; 那张纸有E列; I would like to delete all rows which have an empty field on column E . 我想删除E列上具有空字段的所有行。

Therefore, I wrote this code: 因此,我编写了以下代码:

Sub EmptyCells()
   Sheets("Sheet1").Columns("E:E").SpecialCells(xlBlanks).EntireRow.Delete
End Sub

However, when I run it, I get this error: 但是,当我运行它时,出现此错误:

Subscript out of range 下标超出范围

Why is this error occurring? 为什么会发生此错误? I am sure the column E exists in Sheet1 . 我确定E列存在于Sheet1 Are my references incorrect? 我的引用不正确吗?

EDIT 编辑

If I try Columns("E") or Range("E:E") , instead of Columns("E:E") I get the same error 如果我尝试Columns("E")Range("E:E") ,而不是Columns("E:E")我将得到相同的错误

Your code will work if there are any REAL empty cells in column E ( within UsedRange ). 如果E列( 在UsedRange中有任何REAL空单元格,则您的代码将起作用。

You may have cells that contain formulas returning Null . 您可能具有包含返回Null的公式的单元格。 SpecialCells does not consider them empty. SpecialCells不认为它们为空。 You may have cells in column E containing Null as a constant; 您可能在E列中包含以Null为常数的单元格; SpecialCells also considers them filled. SpecialCells还认为它们已填充。

EDIT#1: 编辑#1:

Based upon your specific error message make sure the worksheet name is spelled correctly . 根据您的特定错误消息, 确保工作表名称拼写正确 Sheet1 is not the same as Sheet 1 Sheet1Sheet 1

I have tried this and it works: 我已经尝试过了,并且有效:

Option Explicit
Sub TetsMe()
    MsgBox (Worksheets(1).Parent.Name & VbCrLf & Worksheets(1).Name)
    Worksheets(1).Columns("E:E").SpecialCells(xlBlanks).EntireRow.Delete
End Sub

The different syntax as the index may refer to a different sheet. 作为索引的不同语法可能引用不同的表。 In this case Worksheets(1) is the first worksheet in the workbook. 在这种情况下, Worksheets(1)是工作簿中的第一个工作表。

The MsgBox() will tell you which worksheet and which workbook you are changing. MsgBox()会告诉您要更改的工作表和工作簿。 Once you know, what you are doing, you may remove it. 知道自己在做什么之后,就可以将其删除。

If the problem is in the workbook name, then it is easy to change it like this: 如果问题出在工作簿名称中,则可以像这样轻松更改它:

With Workbooks("NameYourWorkbook").Worksheets("NameYourWorksheet")
    .Columns("E:E").SpecialCells(xlBlanks).EntireRow.Delete
End With

Your code works if you have a sheet named/titled Sheet1 . 如果您有一个名为/标题为Sheet1工作表,则代码可以工作。

Sub EmptyCells()
   Sheets("Sheet1").Columns("E:E").SpecialCells(xlBlanks).EntireRow.Delete
End Sub

Note that there are different ways to call a sheet. 请注意,有不同的方法来调用工作表。

Sheets("Sheet1") 'means the visible name of the sheet is `Sheet1` (not the same as VBA name)
                 'the number doesn't tell anything about it's position, it's just a name.

Sheet1           'means the VBA name of the sheet.
                 'the number doesn't tell anything about it's position, it's just a name.

Worksheets(1)    'means the first worksheet (which ever is in the first position)

Sheets(1)        'means the first sheet (can also be a chart not only a worksheet)
                 'this has no relation to its name which could be "Sheet5"
                 '(If "Sheet5" is in first position).

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

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