简体   繁体   English

使用Selection.SpecialCells(xlCellTypeVisible)时未选中的单元会受到影响

[英]Non selected cells being affected when using Selection.SpecialCells(xlCellTypeVisible)'

I have created a macro that adds a prefix to the current selection. 我创建了一个为当前选择添加前缀的宏。 It's pretty simple as you can see - it loops through visible cells in the selection. 如您所见,这非常简单-它遍历所选内容中的可见单元格。 I added the .SpecialCells(xlCellTypeVisible) because it was affecting unintended cells when using filters. 我添加了.SpecialCells(xlCellTypeVisible)因为它在使用过滤器时会影响意外的单元格。

Sub Prefix()

Dim rng As Range
Dim Prefix As String
Prefix = "P"

For Each rng In Selection.SpecialCells(xlCellTypeVisible)
    rng = Prefix & rng
Next rng

End Sub

As an example, assume that my data is Cells A1:A4 filled out like this: 例如,假设我的数据是像这样填写的Cells A1:A4

Title
1
2
1

My problem occurs when I do the following: 当我执行以下操作时,会出现我的问题:

  1. Put an Autofilter on the range, and hide the '2' that is in A3 将自动Autofilter放在范围上,并隐藏A3中的“ 2”
  2. Select A2 (the first '1') 选择A2(第一个“ 1”)
  3. Run my macro 运行我的宏

The problem is that instead of affecting only Cell A2 (the selected cell), it starts applying the prefix to the first row (A1:Z1 , etc.) until I cancel the macro. 问题在于,它不仅仅影响Cell A2 (选定的单元格),而是开始将前缀应用于第一行(A1:Z1等),直到我取消宏为止。 It will keep doing this to thousands of cells if I let it keep running. 如果我让它继续运行,它将继续对数千个单元执行此操作。

This problem doesn't happen when my selection is multiple cells, or if I use Selection rather than Selection.SpecialCells(xlCellTypeVisible) , or if I apply the macro to one cell when nothing is being filtered out (hidden). 当我的选择是多个单元格时,或者如果我使用Selection而不是Selection.SpecialCells(xlCellTypeVisible) ,或者在没有任何内容被滤除(隐藏)时将宏应用于一个单元格,则不会发生此问题。

Does anyone have any ideas why the selection defaults to the whole spreadsheet when I only have one cell selected? 没有人有任何想法,为什么当我只选择一个单元格时,所选内容默认为整个电子表格?
Alternatively, can anyone suggest a way to add prefixes like this using VBA? 另外,有人可以建议使用VBA这样添加前缀的方法吗?

I am aware that using an excel formula would alleviate the problem but that is not practical for me and regardless is not as fast as clicking a macro. 我知道使用excel公式可以缓解此问题,但这对我来说并不实际,并且无论如何它都没有单击宏的速度。

您可以使用“ Application.intersect”来确保获得所需的结果:

For each rng in Application.Intersect(Selection,ActiveSheet.Cells.SpecialCells(xlCellTypeVisible))

If you want this to work dynamically when you select a range of cells manually AND your intent is to just prepend the country code from the input box to the current value of the cell, then add the change event posted below to the code section of your worksheet and add the routine to a code module in the workbook. 如果您希望在手动选择一系列单元格时能够动态工作,而您的意图是将国家/地区代码从输入框中添加到该单元格的当前值之前,则将下面发布的change事件添加到您的代码部分工作表并将例程添加到工作簿中的代码模块。

Not sure why you are doing the replace of the minus sign, but you can add it to this code if needed. 不知道为什么要进行减号的替换,但是可以根据需要将其添加到此代码中。

Worksheet code: 工作表代码:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Call AddPrefix(Target)
End Sub

Add a module and put this code in it. 添加一个模块并将此代码放入其中。

Sub AddPrefix(rangeToPrefix)
    Dim cCell As Range
    Dim prefix As String
    prefix = InputBox("Enter your country code", "Set Country Code", +33)

    For Each cCell In rangeToPrefix.SpecialCells(xlCellTypeVisible)
        If Not IsEmpty(cCell) Then cCell.Value = prefix & cCell.Value
    Next cCell
End Sub

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

相关问题 如何在使用 Selection.SpecialCells(xlCellTypeVisible) 时检测何时未选择任何单元格? - How to detect when no cell is selected while using Selection.SpecialCells(xlCellTypeVisible)? Selection.SpecialCells(xlCellTypeVisible).Count 在过滤器产生单行结果时给出溢出错误 - Selection.SpecialCells(xlCellTypeVisible).Count gives overflow error when single row result from filter 尽管使用了`SpecialCells(xlCellTypeVisible)`,但选择可见单元格不起作用 - Selecting Visible Cells is not working although using `SpecialCells(xlCellTypeVisible)` SpecialCells(xlCellTypeVisible)还包括隐藏/过滤的单元格 - SpecialCells(xlCellTypeVisible) also includes hidden/filtered cells 在 excel vba 中选择了 SpecialCells(xlCellTypeVisible) 额外行 - SpecialCells(xlCellTypeVisible) extra row is selected in excel vba Cells.SpecialCells(xlCellTypeVisible)。循环慢速复制 - Cells.SpecialCells(xlCellTypeVisible).Copy slow in loop SpecialCells(xlCellTypeVisible) - SpecialCells(xlCellTypeVisible) Selection.SpecialCells() 方法返回意外范围(Excel VBA) - Selection.SpecialCells() method returns unexpected range (Excel VBA) VBA通过.SpecialCells(xlCellTypeVisible)合并自动过滤的单元格 - VBA merging autofiltered cells via .SpecialCells(xlCellTypeVisible).Range SpecialCells(xlCellTypeVisible)在UDF中不起作用 - SpecialCells(xlCellTypeVisible) not working in UDF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM