Excel 找到已占用的单元格
在Win 10中使用Excel 365 我在查找/识别正在使用的工作表单元格时遇到了一个非常小的问题。例如,在单元格A1和单元格H5中存在常数。单元格B2包含可溢出的动态数组常量:Excel 找到已占用的单元格,excel,vba,office365,Excel,Vba,Office365,在Win 10中使用Excel 365 我在查找/识别正在使用的工作表单元格时遇到了一个非常小的问题。例如,在单元格A1和单元格H5中存在常数。单元格B2包含可溢出的动态数组常量: ={1,2,"",4,5,6;7,8,9,"",11,99;100,"",0,0,100,0} 由于此表包含公式和常量,我尝试了我的trusty: Sub LocateCellsWithStuffInThem() Dim rng As Range With ActiveSheet.Cells
={1,2,"",4,5,6;7,8,9,"",11,99;100,"",0,0,100,0}
由于此表包含公式和常量,我尝试了我的trusty:
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的单元格。它是数组常量的一部分,即使<代码>特殊单元不认为它包含公式或常量,并且具有零长度!
如何为容易定位的占用单元编写代码?我是否必须在
UsedRange
中的所有单元格上循环?您可以循环公式单元格并添加溢出区域(如果有):
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
没有快速解决方案看起来……非常感谢!。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。