我需要從列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