I am trying to find nonempty cells in a defined range variable using .SpecialCells
, if I use:
Set aRange = Range(Cells(2, 5), Cells(botRow, 5))
Set tRange = aRange.SpecialCells(xlCellTypeConstants)
tRange
is Empty. However, if I use:
Set tRange = Range(Cells(2, 5), Cells(botRow, 5)).SpecialCells(xlCellTypeConstants)
tRange
has what I want.
Why doesn't .SpecialCells
work on a defined range variable? Am I missing something?
Try to qualify your objects ( Ranges & Cells
) with a worksheet:
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. This is a combination of the checkboxes you find in the Go To Special dialog. If you want all, do like this:
aRange.SpecialCells(xlCellTypeConstants, xlErrors + xlLogical + xlNumbers + xlTextValues)
This will work with both your versions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.