简体   繁体   English

尽管使用了`SpecialCells(xlCellTypeVisible)`,但选择可见单元格不起作用

[英]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.

相关问题 SpecialCells(xlCellTypeVisible)在UDF中不起作用 - SpecialCells(xlCellTypeVisible) not working in UDF sheet.SpecialCells(xlCellTypeVisible)中的范围也选择了空白行,尽管应用了VBA过滤器 - range in sheet.SpecialCells(xlCellTypeVisible) also selecting blank rows although a filter was applied VBA 使用.Hidden或.SpecialCells(xlCellTypeVisible)来忽略隐藏的行 - 不工作 - Using .Hidden or .SpecialCells(xlCellTypeVisible) to Ignore Hidden Rows — Not Working SpecialCells(xlCellTypeVisible)还包括隐藏/过滤的单元格 - SpecialCells(xlCellTypeVisible) also includes hidden/filtered cells Cells.SpecialCells(xlCellTypeVisible)。循环慢速复制 - Cells.SpecialCells(xlCellTypeVisible).Copy slow in loop SpecialCells(xlCellTypeVisible) - SpecialCells(xlCellTypeVisible) 使用Selection.SpecialCells(xlCellTypeVisible)时未选中的单元会受到影响 - Non selected cells being affected when using Selection.SpecialCells(xlCellTypeVisible)' 从 ListObject 表的特定列中选择 SpecialCells(xlCellTypeVisible) - selecting SpecialCells(xlCellTypeVisible) from specific columns of ListObject table VBA通过.SpecialCells(xlCellTypeVisible)合并自动过滤的单元格 - VBA merging autofiltered cells via .SpecialCells(xlCellTypeVisible).Range 使用.SpecialCells(xlCellTypeVisible) 属性循环时发出隐藏单元格 - Emit hidden cell while looping using .SpecialCells(xlCellTypeVisible) property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM