简体   繁体   English

Excel的SpecialCells方法奇怪的行为

[英]SpecialCells method of Excel Range strange behaviour

I have written a macro to find blank cells from a certain range using SpecialCells method of excel range object. 我已经编写了一个宏来使用excel范围对象的SpecialCells方法查找某个范围内的空白单元格。 When i am trying to execute following code i get a exception as "No cells found". 当我尝试执行以下代码时,我得到一个异常为“找不到单元格”。

Sub test()
Debug.Print Sheet1.Range("A1:D4").SpecialCells(xlCellTypeBlanks).Address
End Sub

Steps:- 脚步:-

  1. Open a new Excel instance. 打开一个新的Excel实例。
  2. Press ALT + F11/ Open VBE 按ALT + F11 /打开VBE
  3. Insert a new Module 插入一个新模块
  4. Paste the above code and run test macro as specified above. 粘贴上面的代码并运行上面指定的测试宏。

Output Received: 收到的输出:

Runtime Error '1004'. 运行时错误'1004'。 No Cells Found. 没有找到细胞。

Output Expected: 产量预期:

$A$1:$D$4 $ A $ 1:$ d $ 4

Case 1: 情况1:

Now if i do any operation of cell A1. 现在,如果我做单元格A1的任何操作。 Operation such as giving fill color, etc. Call the test() macro then it does not throw exception. 给出填充颜色等操作。调用test()宏,然后不抛出异常。

Output Received: 收到的输出:

$A$1 $ A $ 1

Output Expected: 产量预期:

$A$1:$D$4 $ A $ 1:$ d $ 4

Case 2: 案例2:

Suppose if i give any value in cell B3. 假设我在单元格B3中给出任何值。 Call the test() macro, exception is not thrown. 调用test()宏,不抛出异常。

Output Received: 收到的输出:

$B$1:$B$2,$A$1:$A$3 $ B $ 1:$ B $ 2,$ A $ 1:$ A $ 3

Output Expected: 产量预期:

$A$1:$D$4 $ A $ 1:$ d $ 4

Case 3: 案例3:

If i try to edit value or fill the cell outside the range "A1:D4" for example E10 and execute test() method then it gives me the proper output. 如果我尝试编辑值或填充范围“A1:D4”之外的单元格,例如E10并执行test()方法,那么它会给我正确的输出。

Output Received: 收到的输出:

$A$1:$D$4 $ A $ 1:$ d $ 4

Note: Execute each Case with a new excel instance. 注意:使用新的excel实例执行每个Case。

The reason is SpecialCells "looks" at the UsedRange when returning a range. 原因是,当返回范围时, SpecialCells “查看” UsedRange

So using SpecialCells on an unused sheet will return Runtime Error '1004'. No Cells Found. 因此,在未使用的工作表上使用SpecialCells将返回Runtime Error '1004'. No Cells Found. Runtime Error '1004'. No Cells Found. (As an aside always use error handling with SpecialCells) (另外,总是使用SpecialCells进行错误处理)

When you change cell A1, that becomes the sheets UsedRange , hence your Case 1 return of "A1" 当您更改单元格A1时,它将成为工作表UsedRange ,因此您的案例1返回“A1”

The code below looks to return Range("A1:D4").SpecialCells(xlCellTypeBlanks) for 下面的代码看起来返回Range(“A1:D4”)。SpecialCells(xlCellTypeBlanks)for

  1. A blank sheet - fails 空白表 - 失败
  2. The sheet with A10 filled in - A1:A4 填写A10的纸张 - A1:A4
  3. The sheet with A10 cleared - A1:A4 A10清除的纸张 - A1:A4
  4. The sheet with the usedrange reset (which is effectively 1) - fails 具有usedrange重置的表(实际上是1) - 失败

     Sub Test() Dim ws As Worksheet Set ws = Sheets.Add On Error Resume Next 'blank sheet Debug.Print "Blank sheet " & ws.Range("A1:D4").SpecialCells(xlCellTypeBlanks).Address 'enter in A10 ws.[a10] = "test" Debug.Print "With A10 " & ws.Range("A1:D4").SpecialCells(xlCellTypeBlanks).Address 'clear a10 ws.[a10].ClearContents Debug.Print "With A10 cleared " & ws.Range("A1:D4").SpecialCells(xlCellTypeBlanks).Address 'reset range ActiveSheet.UsedRange Debug.Print "With A10 cleared and range reset" & ws.Range("A1:D4").SpecialCells(xlCellTypeBlanks).Address On Error GoTo 0 End Sub 

.SpecialCells work with the used range of a worksheet and not specific cells unless the specific cells fall within the used range. .SpecialCells使用工作表的使用范围而不是特定单元格,除非特定单元格在使用范围内。

To test Specialcells on an entirely new sheet will give you the error because the usedrange is just $A$1. 要在一个全新的工作表上测试Specialcells会给你错误,因为usedrange只是$ A $ 1。 If $A$1 is colored then you will not get an error as expected in CASE 2 above. 如果$ A $ 1被着色,那么您将不会像上面的案例2中那样得到错误。

You have to use Specialcells with utmost care. 你必须非常小心地使用Specialcells Here is an example which will not give you an error on a new sheet. 这是一个不会在新工作表上给出错误的示例。

Sub Sample()
    If ActiveSheet.UsedRange.Column > 1 Or ActiveSheet.UsedRange.Row > 1 Then
        Debug.Print ActiveSheet.UsedRange.SpecialCells(xlCellTypeBlanks).Address
    End If
End Sub

Now type something in Cell D5. 现在在Cell D5中键入内容。 Say "Blah Blah" 说“Blah Blah”

Now run this code 现在运行此代码

Sub Sample()
    Debug.Print Sheet1.Range("A1:D4").SpecialCells(xlCellTypeBlanks).Address
End Sub

You will notice that this will work because the range is within the UsedRange Address. 您会注意到这将起作用,因为范围在UsedRange地址内。

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

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