我有一个过滤范围的宏,我有一系列值,我想表示应用过滤器后选择的行数。
我对大部分代码进行了排序,我只是卡在选择可见行上。 例如。表1包含可变数字(1,2,3,4等),我将其标记为NOC1。
现在应用过滤器后,它会选择正确的行数,但也会选择隐藏的单元格。我只是想让它只选择可见的细胞。
以下是代码:
Set TopVisibleCell = Rstatus.Offset(1).Rows.SpecialCells(xlCellTypeVisible).Rows(1)
TopVisibleCell.Select
Selection.Resize(Selection.Rows.Count + NOC1 - 1, _
Selection.Columns.Count).Copy
非常感谢任何帮助。
谢谢!
编辑:
请原谅我糟糕的描述,似乎我没有表达清楚。 请找到Sample.xlsm的链接,希望能够解释我的问题。
感谢您的帮助
答案 0 :(得分:0)
如果第1行是标题行,并且您想要选择AutoFilter的可见范围,并且没有"垃圾"在列 A 中的过滤器下方,然后:
Sub SelectVisibleA()
Dim NLastVisible As Long, r As Range
NLastVisible = Cells(Rows.Count, "A").End(xlUp).Row
Set r = Range("A2:A" & NLastVisible).Cells.SpecialCells(xlCellTypeVisible)
r.Select
End Sub
将在 A 列中选择可见素材............您需要调整以获取其他列。
答案 1 :(得分:0)
你可以用计数器循环:
Sub FilterCDA()
Dim sh1 As Worksheet
Dim N As Long
Dim TopVisibleCell As Range
Dim sh2 As Worksheet
Dim HeaderRow As Long
Dim LastFilterRow As Long
Dim st As String
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim VTR As String
Dim W As Integer
Dim R As Integer
Dim NOC As Range
Dim NOC1 As Integer
Dim rSelect As Range
Dim rCell As Range
Set sh1 = Sheets("Request")
Set sh2 = Sheets("Request")
C = 2
Set NOC = sh2.Range("D2")
NOC1 = NOC.Value
LR = Worksheets("ORT").Range("A" & Rows.Count).End(xlUp).Row
Set Rstatus1 = Worksheets("ORT").Range("G2:G" & LR)
Set Rstatus = Worksheets("ORT").Range("A1:G" & LR)
N = sh1.Cells(Rows.Count, "C").End(xlUp).Row
Sheets("CSV").Cells.NumberFormat = "@"
For i = 2 To N
v = sh1.Cells(i, 3).Value
If v <> "" Then
st = st & v & ","
End If
Next i
st = Mid(st, 1, Len(st) - 1)
Arr1 = Split(st, ",")
Sheets("ORT").Activate
For i = LBound(Arr1) To UBound(Arr1)
Sheets("ORT").AutoFilterMode = False
With Sheets("ORT").Range("A:G")
.AutoFilter Field:=3, Criteria1:=Arr1(i), Operator:=xlFilterValues
End With
Fr = Worksheets("ORT").Range("C" & Rows.Count).End(xlUp).Row - 1
' No rows filtered then Fr = 0
If Fr > 0 Then
With Rstatus
Set rVis = .Resize(.Rows.Count - 1, 1).Offset(1).SpecialCells(xlCellTypeVisible)
End With
For Each rCell In rVis.Cells
If rSelect Is Nothing Then
Set rSelect = rCell.Resize(, Rstatus.Columns.Count)
Else
Set rSelect = Union(rSelect, rCell.Resize(, Rstatus.Columns.Count))
End If
lCounter = lCounter + 1
If lCounter >= NOC1 Then Exit For
Next rCell
rSelect.Copy
Sheets("CSV").Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
ElseIf Fr = 0 Then
End If
Set NOC = NOC.Offset(1)
NOC1 = NOC.Value
Next i
Sheets("ORT").AutoFilterMode = False
Sheets("Request").Select
Range("E2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF('CSV'!C[-2],'Request'!RC[-2])"
On Error Resume Next
Selection.AutoFill Destination:=Range("E2:E" & Range("C" & Rows.Count).End(xlUp).Row), Type:=xlFillCopy
Columns("E:E").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Sheets("Control").Select
Range("A1").Select
End Sub