Expected end of statement VBA error - excel

I hope you're all doing well!
A quick but tricky question (at least for me)
I have a code that interpolates values according to dates. Basically I want to insert the interpolation value to a cell in the main workbook in the sheet "Deals".
BInterpol is a function that works with a program installed in my computer. In the function, I take dates( D4:D18) linked to values (E4:E18), the following argument is the date that is located in another workbook and sheet and then linear is the method of interpolation. When I write the code below, it gives me Expected end of statement error and it highlights Linear. Any idea what needs to be change so it works ? (I want to make so that the formula is written in the cell needed so the interpolation is done there)
wb2.Sheets("Deals ").Range("V" & x).Value = "=BInterpol('INTERP'!D4:D18,'INTERP'!E4:E18," & wb1.Sheets("New").Range( "I" & j) & " , " Linear " )"

Try with .Formula and concatenate the Linear variable to the string with & as mentioned in the comments:
wb2.Sheets("Deals MTL").Range("V" & x).Formula = "=BInterpol('OIS INTERP'!D4:D18,'OIS INTERP'!E4:E18," & wb1.Sheets("NewTrades").Range( "I" & j) & "," & Linear ")"
Depending on what exactly is Range("I" & j) you may be interested in getting its address and not its value:
wb2.Sheets("Deals MTL").Range("V" & x).Formula = "=BInterpol('OIS INTERP'!D4:D18,'OIS INTERP'!E4:E18," & _
wb1.Sheets("NewTrades").Range("I" & j).Address & _
"," & Linear ")"

Nevermind, got it !
wb2.Sheets("Deals").Range("V" & x).Value = "=BInterpol('INTERP'!D4:D18,'INTERP'!E4:E18," & wb1.Sheets("New").Range( "I" & j) & " , "**"** Linear **"**" )"
The Linear needed two pairs of " for some reason ( don't know why, but that solved it.

Related

VBA, If not empty execute for row

In my script I got the following line.
.Range(.Cells(1, "GP"), .Cells(last, "GP")).FormulaR1C1 = "=ISOWEEKNUM(RC" & i & ")"
However, for an empty row this writes "52" (Since that's the week for an empty date).
Is there a way I can write nothing if the line is empty?
Perhaps use this as your formula:
"=IF(RC" & i & "="""","""",ISOWEEKNUM(RC" & i & "))"
I ended up solving it with:
"=IF(RC" & i & "<>"""",ISOWEEKNUM(RC" & i & "),"""")" to be exact worked

Writing Ranges into formulas with VBA

I am using variable ranges, and want to insert those ranges into a formula that can be saved in a document that does not use VBA.
Do I have to use a with statement?
My If Statement works until I try and insert a relative range. I searched and didn't see much on this, so this issue is probably so simply I am an idiot.
The program will be doing things all over the worksheet, so relative coding is needed. The number of items in the range changes with each iteration. I will be using an end range integer [myRange] for the number instead of 10, but I wanted to simplify what I am struggling with into the smallest steps.
Dim Test As String
Test = "=IF(COUNTIF(" & ActiveCell.Offset(1, 0).Address & ":" & ActiveCell.Offset(10, 0).Address & "" _
& ")," & """Not Complete""" & "," & "" & """"")"
ActiveCell.Formula = Test
Desired output in target cell:
"=IF(COUNTIF($J$3:$J$12),"Not Complete","")"
As mentioned in comments, your COUNTIF statement is currently invalid. What are you counting in the range J3:J12? This needs to be added (in place of "CRITERIA" in below solution) before your equation will be valid
Test = "=IF(COUNTIF(" _
& ActiveCell.Offset(1).Resize(10).Address(True, True) _
& ",""CRITERIA""), ""Not Complete"", """")"
If you want your output to actually have the quotes you can output
Chr(34) & Test & Chr(34) which seems cleaner than wrapping the whole string in quotes from a readability point of view IMO

Excel VBA: inserting WHEN formula into specific range of cells with variable changing column

Hello dear StackOverflow users,
My issue is regarding inserting a Formular via Macro into specific range of cells. Again, again and again.
So the Basic function of the formula should be the following:
=WHEN('ZEK '!F3="X";"6";WHEN('ZEK '!G3="X";"8";"1"))
In my macro it has to Change the column for the next specific range.
This is Happening in a for Loop for i growing.
meaning --> =When('ZEK '!$F$"& i+2 &".......
or in any other Syntax that should work. My try fairly does not, thats why I need your help.
This is my Initial try where i exchanged literally everything with strings:
Sub 123()
Dim a,b,c As String
a = 1
b = 6
c = 8
....
'used and declared variables k, x, f, a, b, c
Range(Cells(k + 2, 5), Cells(k + 27, 5)).FormulaR1C1 = _
"=WHEN('ZEK'!$F$" & f & "=" & x & ";" & a & ";WHEN('ZEK'!$G$" & f & "=" & x & ";" & b & ";" & c & "))"
End Sub
With that i get Runtime Error 1004. (changed it to .Formula from .FormulaR1C1)
I hope i gave enough Information, so that you could help me.
This really does not have to be performant, i just Need to get the formula into about 100.000 cells with i changing for each Range of cells
I think it's a language problem... Try
"=IF('ZEK'!$F$" & f & "=" & x & "," & a & ",IF('ZEK'!$G$" & f & "=" & x & "," & b & "," & c & "))"
Notice the use of IF and , as a delimiter instead of ;. Is WHEN even a valid worksheet function? To be honest I don't know if the worksheet functions get translated or not when setting the formula via VBA. The delimiters are adapted though.
edit: there is also the .FormulaLocal property that should work with ; as delimiter! So change .Formula to .FormulaLocal. This will probably cause an error if you execute it on a machine that uses , as default delimiter so I'd try to stick with .Formula.

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) & ")"

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