在Win 10中使用Excel 365
在定位/识别正在使用的工作表单元格时,我遇到了一个非常小的问题。例如,在单元格A1和cell H5中有常量。Cell B2包含可溢出的动态数组常量:
={1,2,"",4,5,6;7,8,9,"",11,99;100,"",0,0,100,0}
由于此表同时包含公式和常量,所以我尝试了我的信任:
Sub LocateCellsWithStuffInThem()
Dim rng As Range
With ActiveSheet.Cells
Set rng = Union(.SpecialCells(xlCellTypeFormulas), .SpecialCells(xlCellTypeConstants))
End With
MsgBox rng.Address(0, 0)
End Sub
这意味着:
我希望能见到B2:G4,A2,H5
。
显然正在使用像D2这样的单元格。它是数组常量的一部分,尽管SpecialCells
不认为它包含公式或常数,而且长度为零!
如何编写代码以方便地定位被占用的单元格?我必须遍历UsedRange
中的所有单元格吗?
发布于 2020-05-21 16:27:38
您可以循环公式单元格并添加溢出区域(如果有溢出区域):
Sub LocateCellsWithStuffInThem()
Dim rng As Range
Dim rng2 As Range
With ActiveSheet.Cells
Set rng = .SpecialCells(xlCellTypeConstants)
For Each rng2 In .SpecialCells(xlCellTypeFormulas).Cells
If rng2.HasSpill Then
Set rng = Union(rng2.SpillingToRange, rng)
Else
Set rng = Union(rng2, rng)
End If
Next rng2
End With
MsgBox rng.Address(0, 0)
End Sub
https://stackoverflow.com/questions/61938169
复制相似问题