简体   繁体   English

使用具有特殊单元格的定义范围

[英]Using A Defined Range With SpecialCells

I am trying to find nonempty cells in a defined range variable using .SpecialCells , if I use:我正在尝试使用.SpecialCells在定义的范围变量中查找非空单元格,如果我使用:

    Set aRange = Range(Cells(2, 5), Cells(botRow, 5))
    Set tRange = aRange.SpecialCells(xlCellTypeConstants)

tRange is Empty. tRange为空。 However, if I use:但是,如果我使用:

    Set tRange = Range(Cells(2, 5), Cells(botRow, 5)).SpecialCells(xlCellTypeConstants)

tRange has what I want. tRange有我想要的。

Why doesn't .SpecialCells work on a defined range variable?为什么.SpecialCells不适用于定义的范围变量? Am I missing something?我错过了什么吗?

Try to qualify your objects ( Ranges & Cells ) with a worksheet:尝试使用工作表来限定您的对象( Ranges & Cells ):

Dim ws as Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") '<-- Update

Dim aRange as Range, tRange as Range

Set aRange = ws.Range(ws.Cells(2, 5), ws.Cells(botRow, 5))
Set tRange = aRange.SpecialCells(xlCellTypeConstants)

When you are using SpecialCells with xlCellTypeConstants , add the second parameter too.当您将SpecialCellsxlCellTypeConstants一起使用时,也请添加第二个参数。 This is a combination of the checkboxes you find in the Go To Special dialog.这是您在 Go 到特殊对话框中找到的复选框的组合。 If you want all, do like this:如果你想要所有,这样做:

aRange.SpecialCells(xlCellTypeConstants, xlErrors + xlLogical + xlNumbers + xlTextValues)

This will work with both your versions.这将适用于您的两个版本。

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

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