I have a pivot and I want to count 6 cells from last row up, however sometime in the pivot there will be less then 6 cells with a value.
In this case how can I use IF the selection of cells is less then 6 cells with value?
How could I say: IF the selection is less then 6 cells with value Then count the total of cells with value.
Sub Last_6_Months_Frequency()
Dim lastRow As Long
Dim Seltall As Long
Dim Seltallc As Long
Dim cseleall As Long
With Sheet1
lastRow = .Cells(Rows.count, 4).End(xlUp).row
.Range("D3").Formula = "=AVERAGE(D" & lastRow - 6 & ":D" & lastRow - 1 & ")"
if selection < 6 celss then
lastRow = .Cells(Rows.count, 4).End(xlUp).Offset(-1, 0).Select
Seltall = Range(Selection, Selection.End(xlUp)).Select
Seltallc = Application.sum(Selection)
lastRow = .Cells(Rows.count, 4).End(xlUp).Offset(-1, 0).Select
Seltall = Range(Selection, Selection.End(xlUp)).Select
cseleall = WorksheetFunction.count(Selection)
.Range("D3").Value = Application.sum(Seltallc) / WorksheetFunction.count(Selection)
End With