简体   繁体   English

如何在使用 Selection.SpecialCells(xlCellTypeVisible) 时检测何时未选择任何单元格?

[英]How to detect when no cell is selected while using Selection.SpecialCells(xlCellTypeVisible)?

In my Sub I'm using below code to obtain user selected range.在我的 Sub 中,我使用下面的代码来获取用户选择的范围。

Dim rng As Range
Set rng = Selection.SpecialCells(xlCellTypeVisible)

It works fine as long as user selects some cells/range.只要用户选择一些单元格/范围,它就可以正常工作。 However whenever macro is called without selecting any cells, rng overflows and application freezes.但是,每当在未选择任何单元格的情况下调用宏时,rng 就会溢出并且应用程序会冻结。 Is there any way to detect if user hasn't selected any cells and exit safely?.有没有办法检测用户是否没有选择任何单元格并安全退出?

Is a range selected?是否选择了范围?

If TypeOf Selection is Range Then

Does the selected range have visible cells?所选范围是否有可见单元格?

On Error Resume Next 'ignore error if there are no visible cells in the selected range
Set rng = Selection.SpecialCells(xlCellTypeVisible)
On Error Goto 0      'stop ignoring errors

If Not rng Is Nothing then
   'do something with rng
End If

Final Solution that I ended Up Using我最终使用的最终解决方案

Check if selection is a range and CountLarge greater than 1;检查 selection 是否为范围且 CountLarge 是否大于 1; Use ErrorHandler to deal with cases where no visible cell is selected.使用 ErrorHandler 处理没有选择可见单元格的情况。

If TypeOf Selection Is Range And Selection.CountLarge > 1 Then
    On Error GoTo ErrorHandler
    Set rng = Selection.SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
'other bits of code
Else
ErrorHandler: 
'Inform user that macro only works with 2 or more visible cells selected
End If

Credits to @ BigBen for the comment and @ Tim Willams 's Answer感谢@BigBen评论和@Tim Willams回答

暂无
暂无

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

相关问题 使用Selection.SpecialCells(xlCellTypeVisible)时未选中的单元会受到影响 - Non selected cells being affected when using Selection.SpecialCells(xlCellTypeVisible)' Selection.SpecialCells(xlCellTypeVisible).Count 在过滤器产生单行结果时给出溢出错误 - Selection.SpecialCells(xlCellTypeVisible).Count gives overflow error when single row result from filter 使用.SpecialCells(xlCellTypeVisible) 属性循环时发出隐藏单元格 - Emit hidden cell while looping using .SpecialCells(xlCellTypeVisible) property 我的 VBA Selection.SpecialCells(xlCellTypeConstants, 1) cell.Value = cell.Text 中的错误 - Error in my VBA Selection.SpecialCells(xlCellTypeConstants, 1) cell.Value = cell.Text 在 excel vba 中选择了 SpecialCells(xlCellTypeVisible) 额外行 - SpecialCells(xlCellTypeVisible) extra row is selected in excel vba SpecialCells(xlCellTypeVisible) - SpecialCells(xlCellTypeVisible) Selection.SpecialCells() 方法返回意外范围(Excel VBA) - Selection.SpecialCells() method returns unexpected range (Excel VBA) SpecialCells(xlCellTypeVisible)在UDF中不起作用 - SpecialCells(xlCellTypeVisible) not working in UDF SpecialCells(xlCellTypeVisible)-自动过滤器返回零行时出错 - SpecialCells(xlCellTypeVisible) - Error when Autofilter returns zero rows 使用.Hidden或.SpecialCells(xlCellTypeVisible)来忽略隐藏的行 - 不工作 - Using .Hidden or .SpecialCells(xlCellTypeVisible) to Ignore Hidden Rows — Not Working
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM