[英]Selecting Visible Cells is not working although using `SpecialCells(xlCellTypeVisible)`
The idea of my code to Union cells with Interior.Color = vbYellow
in addition to the cells selected manually.除了手动选择的单元格之外,我的代码的想法是使用
Interior.Color = vbYellow
来合并单元格。
Then intersect both ranges into one range from ("C:F").然后将两个范围相交成一个范围(“C:F”)。
The problem: there is a case (described below) that invisiable cells are added to the final range.问题:有一种情况(如下所述)将不可见的单元格添加到最终范围中。
Although I have used SpecialCells(xlCellTypeVisible)
,But the issue still exists every time.虽然我用过
SpecialCells(xlCellTypeVisible)
,但是每次都出现这个问题。
How to produce the issue?如何产生问题?
With the attached picture:附上图片:
(1) select range(“F3:F4”) V-2620
then fill color with yellow. (1) select range(“F3:F4”)
V-2620
然后用黄色填充颜色。
(2) on column C, filter on value 2620
then fill color with yellow for the visible cells from C3 till C8 (2) 在 C 列上,过滤值
2620
,然后用黄色填充从 C3 到 C8 的可见单元格
The result is one range (“C3:F8”), But the expected should be two ranges C3:F4 & C7:F8结果是一个范围(“C3:F8”),但预期应该是两个范围 C3:F4 & C7:F8
On the first comment,I will attach a link for my file and also a video to more clarify the issue.在第一条评论中,我将附上我的文件的链接以及视频以更清楚地说明问题。
In advance, thanks for your help.在此先感谢您的帮助。
Sub Automatic_Save_Selection()
ActiveSheet.AutoFilter.ShowAllData
Dim ws As Worksheet: Set ws = ActiveSheet
Dim crg As Range
Set crg = ws.UsedRange
Set crg = crg.Offset(1, 0).Resize(crg.Rows.Count - 1, crg.Columns.Count) 'UsedRange except first Row
'____Find by Yellow Color_______________________
With Application.FindFormat
.Clear
.Interior.Color = vbYellow
.Locked = True
End With
Dim uRng As Range, cel As Variant, FirstAddress As Variant
Set cel = crg.Find(What:=vbNullString, SearchFormat:=True)
If Not cel Is Nothing Then
FirstAddress = cel.Address
Do
If uRng Is Nothing Then
Set uRng = cel
Else
Set uRng = Union(uRng, cel)
End If
Set cel = crg.Find(What:=vbNullString, after:=cel, SearchFormat:=True)
Loop While cel.Address <> FirstAddress
End If
'_____Union (Find by Yellow Color) with Manual Selection__________________
Dim rng As Range
If Not uRng Is Nothing Then
Set rng = Union(Selection.SpecialCells(xlCellTypeVisible), uRng)
Else
Set rng = Selection.SpecialCells(xlCellTypeVisible)
End If
Dim TrimmedRange As Range
Set TrimmedRange = Intersect(rng, ws.UsedRange.Offset(1))
Intersect(TrimmedRange.EntireRow, ws.Range("C:F")).Select
End Sub
As I said in my above comment, the code logic is OK, but it looks that using Find
with searchingFormat
parameter, the filter is automatically switched to ShowAllData
... That's why, Selection.SpecialCells(xlCellTypeVisible)
returned all cells on the respective column , not the (filtered) ones as you imagined.正如我在上面的评论中所说,代码逻辑还可以,但看起来使用带有
searchingFormat
参数的Find
,过滤器会自动切换到ShowAllData
...这就是为什么, Selection.SpecialCells(xlCellTypeVisible)
返回了相应列上的所有单元格,而不是您想象的(过滤的)。
In order to obtain what you want supposing that you need it, not only playing with VBA, you should filter again the range in code, after the Find
part loop:为了获得你想要的东西,假设你需要它,不仅玩 VBA,你应该在
Find
part 循环之后再次过滤代码中的范围:
If so, please include the next line after or before Dim rng As Range
:如果是这样,请在
Dim rng As Range
之后或之前包含下一行:
ws.Range("C2:F8").AutoFilter field:=1, Criteria1:="2620"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.