我需要从列N2和M2中获取值,使用自动筛选器后,赋值只给出了整个工作表中不存在于自动筛选器范围内的值。excel vba将单元格值赋给变量来自自动筛选器结果
Sub mainSub()
Dim fRngb as Range
For Each key In fCatId.Keys
With wshcore
llastrow = wshcore.Range("A" & Rows.Count).End(xlUp).Row
.AutoFilterMode = False
.Range("A1:N" & llastrow).AutoFilter
.Range("A1:N" & llastrow).AutoFilter Field:=1, Criteria1:=fCatId(key)
min = WorksheetFunction.Subtotal(5, Range("H:H"))
max = WorksheetFunction.Subtotal(4, Range("H:H"))
'This does not work. it gives the first 13,2 value not the filtered one.
Set fRngb = wshcore.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
'MsgBox fRngb.Cells(13, 2)
'I've also tried this:
'Range("K2:K2").CurrentRegion.Value(2)
Debug.Print fRngb.Cells(13, 2) & " - " & Range("K2:K2").CurrentRegion.Value(2)
End With
Next key
End Sub
有什么建议吗?
的过滤器后,你可以复制列范围(例如'.Range(“N2:M”&llastrow).Copy'并将其粘贴到某处。这将复制可见单元格。 – Slai