In Sheet1 I have a button that creates in Sheet2 a calculation in cell C10 that I want to be =AverageIF($C$8:$C$9; "<>0"). Pretty easy, isn’t it ?
Well naaaahhh … after many hours googling and testing different approaches, I’m still unable to make it work. Here are my attempts and the error messages associated with it.
‘ First, here are the defined variables
Dim FormulaText as String ‘ I gradually prepare the formula in strings before sending it to the cell : usefull when I have very complex formula
Dim MSN as WorkSheet ‘ that’s the target worksheet where the formula will be sent
Dim row, col as Integer ‘ yep … exactly what you think it is …
‘Now let’s start with the code
row = 10
col = 3
‘ Step 1 : Build the range text to see if that part work
FormulaText = MNS.Cells(row - 2, col).Address & ":" & MNS.Cells(row - 1, col).Address ' at this point FormulaText returns $C$8:$C$9 which is what I expect to get
‘ Step 2 : Build the formula itself
FormulaText = "AverageIF(" & FormulaText & "; ""<>0"")" 'here FormulaText returns AverageIF($C$8:$C$9; "<>0") which is again what I expect to get
‘ Step 3 : Test that formula to the target cell without the = to see if the string is still right
MNS.Range(Cells(10, col).Address).Value = FormulaText ‘ which returns the string AverageIF($C$8:$C$9; "<>0") in C10 as expected.
‘So far so good. And if I manually add the missing = into Sheet2 C10 cell, the formula becomes =AverageIF($C$8:$C$9; "<>0") and it does exactly what I expect it to do
‘Now let’s go back and try to make it work with code from Sheet1
‘ When in Step 2 I build the formula to add the missing =, FormulaText returns =AverageIF($C$8:$C$9; "<>0") which is again what I expect to get (tested with msgbox)
‘but when I send it to C10 my problems begin
MNS.Range(Cells(10, col).Address).Value = FormulaText ‘ returns Application-defined or Object-defined error
MNS.Range(Cells(10, col).Address).Formula = FormulaText ‘ returns same error Application-defined or Object-defined error
MNS.Range(Cells(10, col).Address).FormulaArray = FormulaText ‘ returns Unable to set the FormulaArray property of the Range class
‘ ok, maybe I need to add curled brackets to the formula so let’s say I have this line just after Step 2
FormulaText = "={" & FormulaText & "}" ‘ it returns ={AverageIF($C$8:$C$9; "<>0")} as expected
‘ but when I send it to C10, nothing change, same set of error messages as below.
‘ Now when I make that formula a little bit simpler or far more complex (!!!), it works :
‘ If in Step2 I go for the Average formula instead
FormulaText = "=Average(" & FormulaText & ")" ‘ it returns =AVERAGE($C$8:$C$9) as expected
' and Step3
MNS.Range(Cells(10, col).Address).Value = FormulaText ‘ works perfectly well (same with .Formula and .FormulaArray)
‘ I also used the same recipe in a far more complex formula and similar context (I mean a button in Sheet1 creates the formula for Sheet2)
‘ Step 2 was
FormulaText = "RANK(R" & row - 2 & "C" & col & "," & Chr(39) & MNS.Name & Chr(39) & "!R" & row - 2 & "C3:R" & row - 2 & "C" & StudentNumber & ")"
FormulaText = "=IF(R" & row - 2 & "C" & col & "<>0," & FormulaText & ","""")"
‘ which in the end returned something nice like =IF($C$28<>0;RANK($C$28;'TPGr01'!$C$28:$U$28);"") and that too worked well.
So, what did I miss with my AverageIf formula ? Thanks in advance for all ideas/hints/solutions/inspirations.
Well naaaahhh … after many hours googling and testing different approaches, I’m still unable to make it work. Here are my attempts and the error messages associated with it.
‘ First, here are the defined variables
Dim FormulaText as String ‘ I gradually prepare the formula in strings before sending it to the cell : usefull when I have very complex formula
Dim MSN as WorkSheet ‘ that’s the target worksheet where the formula will be sent
Dim row, col as Integer ‘ yep … exactly what you think it is …
‘Now let’s start with the code
row = 10
col = 3
‘ Step 1 : Build the range text to see if that part work
FormulaText = MNS.Cells(row - 2, col).Address & ":" & MNS.Cells(row - 1, col).Address ' at this point FormulaText returns $C$8:$C$9 which is what I expect to get
‘ Step 2 : Build the formula itself
FormulaText = "AverageIF(" & FormulaText & "; ""<>0"")" 'here FormulaText returns AverageIF($C$8:$C$9; "<>0") which is again what I expect to get
‘ Step 3 : Test that formula to the target cell without the = to see if the string is still right
MNS.Range(Cells(10, col).Address).Value = FormulaText ‘ which returns the string AverageIF($C$8:$C$9; "<>0") in C10 as expected.
‘So far so good. And if I manually add the missing = into Sheet2 C10 cell, the formula becomes =AverageIF($C$8:$C$9; "<>0") and it does exactly what I expect it to do
‘Now let’s go back and try to make it work with code from Sheet1
‘ When in Step 2 I build the formula to add the missing =, FormulaText returns =AverageIF($C$8:$C$9; "<>0") which is again what I expect to get (tested with msgbox)
‘but when I send it to C10 my problems begin
MNS.Range(Cells(10, col).Address).Value = FormulaText ‘ returns Application-defined or Object-defined error
MNS.Range(Cells(10, col).Address).Formula = FormulaText ‘ returns same error Application-defined or Object-defined error
MNS.Range(Cells(10, col).Address).FormulaArray = FormulaText ‘ returns Unable to set the FormulaArray property of the Range class
‘ ok, maybe I need to add curled brackets to the formula so let’s say I have this line just after Step 2
FormulaText = "={" & FormulaText & "}" ‘ it returns ={AverageIF($C$8:$C$9; "<>0")} as expected
‘ but when I send it to C10, nothing change, same set of error messages as below.
‘ Now when I make that formula a little bit simpler or far more complex (!!!), it works :
‘ If in Step2 I go for the Average formula instead
FormulaText = "=Average(" & FormulaText & ")" ‘ it returns =AVERAGE($C$8:$C$9) as expected
' and Step3
MNS.Range(Cells(10, col).Address).Value = FormulaText ‘ works perfectly well (same with .Formula and .FormulaArray)
‘ I also used the same recipe in a far more complex formula and similar context (I mean a button in Sheet1 creates the formula for Sheet2)
‘ Step 2 was
FormulaText = "RANK(R" & row - 2 & "C" & col & "," & Chr(39) & MNS.Name & Chr(39) & "!R" & row - 2 & "C3:R" & row - 2 & "C" & StudentNumber & ")"
FormulaText = "=IF(R" & row - 2 & "C" & col & "<>0," & FormulaText & ","""")"
‘ which in the end returned something nice like =IF($C$28<>0;RANK($C$28;'TPGr01'!$C$28:$U$28);"") and that too worked well.
So, what did I miss with my AverageIf formula ? Thanks in advance for all ideas/hints/solutions/inspirations.