How to get the average of x data in a specific range - excel

I am doing an average of data (VBA Excel) as below:
If n < 8 Then
Sheet2.Cells(i, 20).Value = "=SUM(E" & i & ":S" & i & ")/" & n
Else
Sheet2.Cells(i, 20).Value = "=AVERAGE(LARGE(E" & i & ":S" & i & ", {1,2,3,4,5,6,7,8}))"
End If
n = 0
This code is working, because I want the best 8 values of 15 values.
My question is how to do the same with x values (x will be introduced via a userform, 0<x<16).
Of course, I could use a Select Case with 15 lines depending of x, but it does not seem to me a good coding.
Any ideas?

If AVERAGEIF is available:
Sheet2.Cells(i, 20).Value = "=AVERAGEIF(E" & i & ":S" & i & ","">=""&LARGE(E" & i & ":S" & i & "," & x & "))"
For i = 2 and x = 8, the formula is:=AVERAGEIF(E2:S2,">="&LARGE(E2:S2,8))
If not:
Sheet2.Cells(i, 20).Value = "=SUMIF(E" & i & ":S" & i & ","">=""&LARGE(E" & i & ":S" & i & "," & x & "))/" & x
For i = 2 and x = 8, the formula is:=SUMIF(E2:S2,">="&LARGE(E2:S2,8))/8

Related

IF formula in VBA only returning True or False, not the criteria values

I am having trouble figuring out why my IF formula in VBA is not returning any of the criteria values. All I can get is the True or False value. This is what I currently have:
Cells(y, 11).Formula = "=IF((A" & y & ")+(B" & y & "))-((A" & y - 1 & ")+(B" & y - 1 & "))" > "($J$7*20),(((C" & y & ")-(C" & y - 1 & ")) * $J$7),(((A" & y & ")+(B" & y & "))-((A" & y - 1 & ")+(B" & y - 1 & ")))"
Im sure its just a type-o or I am missing some quotations somewhere but if anyone can take a look at this and find what I am missing, that would be great.
This is my whole code for better context.
Sub make_new_entry()
Dim Layer_Increment, i As Integer
Set Layer_Increment = Cells(7, 2)
y = 10 'Set this to the first row number that gets a time entered.
i = 0
Do While i = 0
If Cells(y, 1) = "" Then
add_row y
Cells(y, 1) = Format(Now(), "m/d/yy")
Cells(y, 2) = Format(Now(), "h:mm")
Cells(y, 3) = Cells(y - 1, 2) - ((Cells(y - 1, 2) - Layer_Increment * (y - 10)))
Cells(y, 8).Formula = "=($H$7*(C" & y & "))+((0.5*'Data Reference'!F2)+(0.4*'Data Reference'!F3)+(0.3*'Data Reference'!F4)+(0.2*'Data Reference'!F5)+(0.1*'Data Reference'!F6)+(0.05*'Data Reference'!F7)+(0.01*'Data Reference'!F8)+(0.001*'Data Reference'!F9))+ $H$10+$N$7"
Cells(y, 9).Formula = "=$I$10-(0.05*(C" & y & "-$C$10))"
Cells(y, 10).Formula = "=(((A" & y & ")+(B" & y & "))-((A" & y - 1 & ")+(B" & y - 1 & ")))/((C" & y & ")-(C" & y - 1 & "))"
Cells(y, 11).Formula = "=IF(A" & y & " + B" & y & ")-(A" & y - 1 & " + B" & y - 1 & ") > $J$7*20, (C" & y & " - C" & y - 1 & ") * $J$7, (A" & y & " + B" & y & ")-(A" & y - 1 & " + B" & y - 1 & ")"
Cells(7, 6).Formula = "=((130-(H" & y & "))/$H$7)-('Build Information'!$F$6-(C" & y & "))"
Cells(7, 4).Formula = "=Max(C:C)"
Cells(7, 10).Formula = "=AverageIf(J11:J" & y & ",""<00:05:00"")"
Cells(8, 11).Formula = "=Sum($K$10:(K" & y & ")"
i = 1
Else
y = y + 1
End If
Loop
End Sub
I suggest that you:
fully qualify your ranges
check your parenthesis and the order the calculations are made in Excel
use variable names that are easy to understand (for example, instead of y use counter) .
Regarding your formula, here is the way I'd fix it
Cells(y, 11).Formula = "=IF((A" & y & "+B" & y & ")-(A" & y - 1 & "+B" & y - 1 & ") > $J$7*20,((C" & y & "-C" & y - 1 & ") * $J$7),((A" & y & "+B" & y & ")-(A" & y - 1 & "+B" & y - 1 & ")))"
PS To fully qualify a range use this syntax
Thisworkbook.Worksheets("Sheet1").Cells(y,11).Formula =

AverageIf formula through VBA macro returns "False" instead of a value

Im trying to use the AverageIf function in my code that uses a cell as criteria. This cell is also in the same macro. When I do this I get "False" returned instead of a value. This is what I have. {Sub make_new_entry()
Dim Layer_Increment, i As Integer
Set Layer_Increment = Cells(7, 2)
y = 10 'Set this to the first row number that gets a time entered.
i = 0
Do While i = 0
If Cells(y, 1) = "" Then
add_row y
Cells(y, 1) = Format(Now(), "m/d/yy")
Cells(y, 2) = Format(Now(), "h:mm")
Cells(y, 3) = Cells(y - 1, 2) - ((Cells(y - 1, 2) - Layer_Increment * (y - 10)))
Cells(y, 8).Formula = "=($H$7*(C" & y & "))+((0.5*'Data Reference'!F2)+(0.4*'Data Reference'!F3)+(0.3*'Data Reference'!F4)+(0.2*'Data Reference'!F5)+(0.1*'Data Reference'!F6)+(0.05*'Data Reference'!F7)+(0.01*'Data Reference'!F8)+(0.001*'Data Reference'!F9))+ $H$10+$N$7"
Cells(y, 9).Formula = "=$I$10-(0.05*(C" & y & "-$C$10))"
Cells(y, 10).Formula = "=((((A" & y & ") + (B" & y & "))-((A" & y - 1 & ")+(B" & y - 1 & ")))*1440)/((C" & y & ") - (C" & y - 1 & "))"
Cells(y, 11).Formula = "=((B" & y & ") + (A" & y & ")) - ($B$10+$A$10)"
Cells(7, 6).Formula = "=((130-(H" & y & "))/$H$7)-('Build Information'!$F$6-(C" & y & "))"
Cells(7, 4).Formula = "=Max(C:C)"
Cells(1, 15).Formula = "=(J" & y - 1 & ")+1.5"
Cells(7, 10).Formula = "=AverageIf(J11:(J" & y & ")," < " &O1)"
i = 1
Else
y = y + 1
End If
Loop
End Sub}
Can anyone help me figure out what Im doing wrong.
You need to double up the quotes around the < to actually include it in the string (it is currently outside the quotes and thus a comparison operator). Then, your parentheses are off:
Cells(7, 10).Formula = "=AverageIf(J11:J" & y & ",""<""&O1)"

excel VBA concatenate function

First, I'm new to programming VBA to excel so if this program makes no sense...here's to the learning curve.
I'm trying to concatenate a range from "BO1:BQ" & DLimportLastRow, where DLimportLastRow is the last row of a variable import that was just run before this code. I've done as much internet research as i can, but most of the concatenate codes i found make no sense to me so i can't modify them to meet my needs. The end goal is to concatenate BO1:BQ" & DLimportLastRow with each cell separated by a "," and fitting into one cell which is "AE" & LastRow, where LastRow is the next blank cell in "AE".
DLimportLastRow = WorkEnd.Cells(WorkEnd.Rows.Count, "BO").End(xlUp).Offset(1).Row
DLimportLastRow = "BQ" & DLimportLastRow
i = 1
Do Until IsEmpty(Range("BO" & i).Value) = True Or Range("C" & i).Value = "" Or Range("C" & i).Value = Null Or Range("C" & i).Value = 0
x = Workbooks(WName).Worksheets("ReportGroupingInfo").Range("BO" & i).Value
For Each cell In Range("BO1", DLimportLastRow)
y = x & cell.Value & ","
i = i + 1
Next
Loop
Range("AE" & LastRow).Value = y
Got it:
Dim y as string
i = 1
Do Until IsEmpty(Range("BO" & i).Value) = True Or Range("C" & i).Value = "" Or Range("C" & i).Value = Null Or Range("C" & i).Value = 0
For Each cell In Range("BO1", DLimportLastRow)
y = y & cell.Value & ","
i = i + 1
Next
Loop
Range("AE" & LastRow).Value = y

How to sum total row and the bottom of the data

Below is my lines of code
sub test
Dim VR As Long
VR = Range("H" & Rows.Count).End(xlUp).Row
Range("J" & Rows.Count).End(xlUp).Offset(2, 0).Formula = "=sum(J2:J" & VR &
")"
Range("L" & Rows.Count).End(xlUp).Offset(2, 0).Formula = "=sum(L2:L" & VR &
")"
Range("M" & Rows.Count).End(xlUp).Offset(2, 0).Formula = "=sum(M2:M" & VR &
")"
Range("N" & Rows.Count).End(xlUp).Offset(2, 0).Formula = "=sum(N2:N" & VR &
")"
Range("O" & Rows.Count).End(xlUp).Offset(2, 0).Formula = "=sum(O2:O" & VR &
")"
Range("P" & Rows.Count).End(xlUp).Offset(2, 0).Formula = "=sum(P2:P" & VR &
")"
Range("Q" & Rows.Count).End(xlUp).Offset(2, 0).Formula = "=sum(Q2:Q" & VR &
")"
Range("R" & Rows.Count).End(xlUp).Offset(2, 0).Formula = "=sum(R2:R" & VR &
")"
Range("S" & Rows.Count).End(xlUp).Offset(2, 0).Formula = "=sum(S2:S" & VR &
")"
Range("T" & Rows.Count).End(xlUp).Offset(2, 0).Formula = "=sum(T2:T" & VR &
")"
Range("U" & Rows.Count).End(xlUp).Offset(2, 0).Formula = "=sum(U2:U" & VR &
")"
Range("V" & Rows.Count).End(xlUp).Offset(2, 0).Formula = "=sum(V2:V" & VR &
")"
end sub
I have these lines of code that sum up every column of data but how do I sum these cells?
Try, please:
Range("X" & VR + 2).formula = "=sum(J" & VR + 2 & ":V" & VR + 2 & ")"
Not an answer to your question but a probably a huge simplification, to insert the formula into all the columns in one step:
Dim VR As Long
VR = Range("H" & Rows.Count).End(xlUp).Row
Dim VR2 As Long
VR2 = VR + 2 ' I'm assuming that you just need to offset the last row by 2
Range("J" & VR2 & ",L" & VR2 & ":V" & VR2).Formula = "=sum(J2:J" & VR & ")"
You have range of columns which you want to sum. For each column you want to:
sum all values that exist in a column,
write sum 2 cells below last row.
Currently, you get the last row for H column and apply it to every column, which might not be valid. Just imagine that last row for H column is 25th and for J column it is 27th. You would write sum of J2:J25 in cell J27 and overwrite value in J27.
So I recommend writing loop over set of columns and apply listed above actions:
cols = Array("J", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V")
rowsCnt = Rows.Count
' This variable will contain sum of all values
totalSum = 0
For i = LBound(cols) to UBound(cols)
lastRow = Range(cols(i) & rowsCnt).End(xlUp)
colSum = Application.Sum(Range(cols(i) & "2"), Range(cols(i) & lastRow))
Range(cols(i) & (lastRow + 2)) = colSum
' Here you sum all column sums
totalSum = totalSum + colSum
Next

VBA coding using last used column number

I'm trying to write VBA code to sum up cells, in the process i calculate the latest updated column using the statement:
clncnt1 = wb.Worksheets("Sheet1").Range("XFD16").End(-4159).Column
Suppose I capture the clncnt1 as 20 which is column P. Now how do I use:
wb.Worksheets("sheet1").Range("A1").Offset(16, clncnt).Formula = "=sum(*P12:P13*)"
using the column number clncnt1 that I just captured?
replace your sum with below
"=sum(" & Cells(12, clncnt1 ).Address & ":" & Cells(13, clncnt1 ).Address & ")"
you are actually getting the addresses of the range and then concatenating it all together.
Full code:
clncnt1 = Worksheets("sheet1").Range("XFD16").End(-4159).Column
Worksheets("sheet1").Range("A1").Offset(16, clncnt1).Formula = "=sum(" & Cells(12, clncnt1).Address & ":" & Cells(13, clncnt1).Address & ")"
this worked wb.Worksheets("sheet1").Range("A1").Offset(11, clncnt + 3).Formula = "=sum(" & _ wb.Worksheets("sheet1").Range("A1").Offset(8, clncnt + 3).Address & ":" & _ wb.Worksheets("sheet1").Range("A1").Offset(9, clncnt + 3).Address & ")"

Resources