[英]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: 当我执行以下操作时,会出现我的问题:
Autofilter
on the range, and hide the '2' that is in A3 Autofilter
放在范围上,并隐藏A3中的“ 2” 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.