Excel VBA .Formula - syntax error adding absolute reference to cell - excel

I have a syntax error in this VBA code:
students.Cells(studentRow, StartColumn).Formula = _
"=If($" & SurnameColumnStr & studentRow & " <> """", if($" & colonnaStr & studentRow & _
" <> """", $" & colonnaStr & " & $" & pointRow & ", 0), """")"
I can't put right this piece of code: $" & colonnaStr & " & $" & pointRow & " (it would be like having "$B$3")
I've tried several ways with no luck.
Any help would be really appreciated.

To get "$B$3" out of colonnaStr (a column reference) and pointRow (a row reference), you will need to use Range.Address.
Using this inside your Formula String:
Range(colonnaStr & pointRow).Address(True, True, xlA1)
Will give you $B$3 as a result.
To learn more about is, read HERE

Related

Quotes in VBA formula

I'm trying to figure out what i'm doing wrong i'm trying to add quotation marks to "engine oils" below. but it keeps giving me an error.
can anyone see what i'm doing wrong?
Sheets("PDF Copy").Cells(t, 6).Formula = "=IF(Daily!I8" & i & "=""ENGINE OILS"" ,DAILY!I" & i & ",Daily!K" & i
You are missing a closing bracket.
'=IF(Daily!I810="ENGINE OILS",Daily!I10,Daily!K10)
Sheets("PDF Copy").Cells(2, 6).Formula = _
"'=IF(Daily!I8" & i & "=""ENGINE OILS"" ,DAILY!I" & i & ",Daily!K" & i & ")"

Weighted average rate in VBA

I am quite new to VBA so I am sorry if my question might seems very trivial.
I would love to write a function in VBA which helps to estimate weighted average rate (for loan portfolio, for instance). I wrote the following VBA code:
Function WAIRS(Amount As Range, InterestRate As Range, MatchRange As Range, Match1)
WAIRS = Evaluate("=SUMPRODUCT(--(""" & MatchRange & """ = """ & Match1 & """),""" & Amount & """, """ & InterestRate & """)") /Application.WorksheetFunction.SumIfs(Amount, MatchRange, Match1)
End Function
The problem is that when I run this function in Excel by adding respective function criterias I get an "#VALUE#". I have tried a lot but cannot find out what is wrong. I Would highly appreciate if you can help me.
Thank you in advance.
Best,
Jeyhun
The string you build for Evaluate should (in this case) not include literal double quotes. Instead of quoting the result of a range value
"""" & MatchRange & """"
...you should retrieve the address notation of that range, and use that without wrapping it in quotes:
MatchRange.Address()
If you apply that consistently, it would make the Evaluate part of the formula look like this:
"=SUMPRODUCT(--(" & MatchRange.Address() & " = " & Match1.Address() & "), " & _
Amount.Address() & ", " & InterestRate.Address() & ")"
When range is another sheet:
The above will not work if your ranges are on another sheet. In that case, I would suggest to create this function:
Public Function fullAddr(range As Range)
fullAddr = "'" & range.Parent.Name & "'!" & _
range.Address(External:=False)
End Function
And then in your formula:
"=SUMPRODUCT(--(" & fullAddr(MatchRange) & " = " & fullAddr(Match1) & "), " & _
fullAddr(Amount) & ", " & fullAddr(InterestRate) & ")"

Run-time Error 1004 with Vlookup

I want to create the below formula in Excel using VBA:
=IF(ISERROR(VLOOKUP($F4,'17402'!$B:$BD,'17402'!AS$50,0))=TRUE,"",(VLOOKUP($F4,'17402'!$B:$BD,'17402'!AS$50,0)))
This is my code:
Dim wsFormula As String
wsFormula = "=IF(ISERROR(VLOOKUP($F4," & "" '"" var_SN & ""'!"" & "$B:$BD," & ""'"" & var_SN & ""'!"" & "AS$50,0))=TRUE,0,(VLOOKUP($F4," & ""'"" var_SN & ""'!"" & "$B:$BD," & ""'"" var_SN & ""'!"" & "AS$50,0)))"
ActiveCell.Formula = wsFormula
Where “var_SN” is “17402”. I receive a Run-time error 1004, Application-defined or Object Define error error, so I wonder if the variable is the problem. I read about double stacking the quotes around ' and ! but that doesn't help.
I also wonder if the lookup value $F2 will become a problem for me as the row reference changes each time I run the macro. For instance, if I am in row 4, I do not want this code writing a formula that would reference cell F1 as the lookup value. Thanks much.
You have too many "
Every where you have 17402 change to " & var_SN & ".
And the "" Needs to be """"
"=IF(ISERROR(VLOOKUP($F4,'" & var_SN & "'!$B:$BD,'" & var_SN & "'!AS$50,0))=TRUE,"""",(VLOOKUP($F4,'" & var_SN & "'!$B:$BD,'" & var_SN & "'!AS$50,0)))"
But you also do not need the IF and ISERROR. You can use the IFERROR:
"=IFERROR(VLOOKUP($F4,'" & var_SN & "'!$B:$BD,'" & var_SN & "'!AS$50,0),"""")"
The IFERROR will do the first argument unless there is an error then it does the second argument.

how to add a test for non-black cells using `FormulaR1C1' in excel vba

I'm trying to insert the following equation into a sheet:
=COUNTIFS(email_logs_output.csv!$AB$2:$AB$26731, _
usersFullOutput.csv!S26, _
email_logs_output.csv!$R$2:$R$26731, _
"<>"&"", _
email_logs_output.csv!$H$2:$H$26731, _
"gift*")
I'm getting stuck at the criteria "<>"&"". Here's what I have:
With Worksheets(users_sheet)
equation_range.FormulaR1C1 = "=COUNTIFS(" & emails_sheet & "!R2C" & email_col & ":R" & rows_email & "C" & email_col & " , RC[" & col_back & "], " _
& emails_sheet & "!R2C18:R" & rows_email & "C18, "" " <> " & "" "")"
End With
When I run it, all I get is TRUE in every cell in equation_range.
How do I get the equation to include "<>"&""?
Just tried one formula and this works
[a1].FormulaR1C1 = "=countif(R1C3,""<>"" & ""a"")"
So in your case
...C18,""<>"" & "" "")"
Notes
Basicaly, whenever you have "text" in your formula just put ""text"" and that should do the trick.
Another thing : Beware of the "autospreading" of the code in VBA environment. Check for non wanted spaces added automaticaly where you may not want them.

Creating a VBA formula using CountIF

I am creating a formula to count based upon 2 conditions. My logic is wrong - again.
When I (manually) enter enter code the summation formula (COUNTIFS) into correct cell itself, it sums correctly:
COUNTIFS(E4:E1362,"Requirement",S4:S1362, "<>4")
When I execute the following code line, I do not get any errors, but instead, all the sums are zero.
Range("G" & Start(groups) - 1).Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & "," & "Requirement" & ",S" & Start(groups) & ":S" & Finish(groups) & "," & Chr(34) & "<>4" & Chr(34) & ")"
I realized that the COUNTIFS was comparing the value in column S to the string "<>4". And none of the cells contain that string. This is why all my values were zero. I do not want the comparison to be against that string. I want the comparison to be column S value NOT equal to 4.
So, I changed the line to (and variations to get it to work):
Range("G" & Start(groups) - 1).Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & "," & "Requirement" & ",S" & Start(groups) & ":S" & Finish(groups) & "," & Chr(34) & "<>" & Chr(34) & "4)"
So, I am at a loss. (1) I manually enter the formula and it works. (2) I build it in VBA, and it does not sum correctly. (3) I look up how to do build the formula correctly, and I get errors time and time again. It seems like the only way to get this to work is to keep the quotes within quotes, but I don't want to compare against the string.
Does this make sense? I'm not liking my worksheet anymore. It is no longer any fun at all. :(
Thank you so much.
I think all the Chr(34) are getting in the way of you viewing your formula correctly. try using "" to make the quote for the formula.
.Formula = "=COUNTIFS(E4:E1362,""Requirement"",S4:S1362,""<>4"")"
with your Start and Finish functions, that would change the formula to:
.Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & _
",""Requirement"",S" & Start(groups) & ":S" & Finish(groups) & ",""<>4"")"
Well, One of the problem I found there was quite simple, instead using "," you must use ";".
It depends on the version you used, some using "," and some others using ";". you can just try it, I hope it solved your problem.
and second one "Requirement" there, I think you should use double quote instead.
and for your case To meet This condition:
COUNTIFS(E4:E1362,"Requirement",S4:S1362, "<>4")
Use This:
"=COUNTIF(E" & Start(groups) & ":" & "E" & Finish(groups) & "," & """Requirement""" & "," & "S" & Start(groups) & ":" & "S" & Finish(groups) & "," & """<>4"")"
I think it Should Work (if I don't miss any Quotes thought).
... Try removing the chr(34) and the extra quotations - It should look exactly like the "Requirement" criteria:
Range("G" & Start(groups) - 1).Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & ",""Requirement"",S" & Start(groups) & ":S" & Finish(groups) & ",""<>4"")"
After running a quick test with your initial code
Range("G1").Formula = "=COUNTIFS(E1" & ":E20" & "," & "Requirement" & ",S1" & ":S20" & "," & Chr(34) & "<>4" & Chr(34) & ")"
Modified slightly to account for your groups variable cell G1 ends up with the following formula:
=COUNTIFS(E1:E20,requirement,S1:S20,"<>4")
which when tested works absolutely fine for your second if statement S1:S20,"<>4".
I would guess that the problem lies with your first statement. What is in your requirement range and what data are you trying to match with.
I set up the requirement named range to be cell D5, I entered a 5 in there and the formula would increment each time I added an extra 5 to the range E1:E20.
I then started typing 4's into the range S1:s20 and this decreased my count.
edit
The last thing that I can suggest is to add in a helper column in column F that evaluates row to see if it matches the requirement condition, then it would be a simple matter of making your formula:
=COUNTIFS(F1:F20,TRUE,S1:S20,"<>4")

Resources