.Formula brings error with punctuation marks vba [duplicate] - excel

This question already has an answer here:
Different languages issue when inserting formula from VBA
(1 answer)
Closed 3 years ago.
I have a problem with adding formula to a cell through VBA.
Everything is ok till the moment when I add punctuation mark like "(" to it.
What I'm doing wrong here?
I've tried already using chr() function and it doesn't work.
Do Until ws.Cells(i, 1) = ""
If ws.Cells(i, 7).Value <> "" Then
If ws.Cells(i, 7).Value <> 0 Then
ws.Cells(i, 7) = 200
ws.Cells(i, 8).Value = 0
ws.Cells(i, 9).Value = 0
ws.Range("J" & i).Formula = "=IF(H" & i & "-F" & i & "<=0;0;H" & i & "-F" & i & ")"
Regards,
Ukalo

Replace:
ws.Range("J" & i).Formula = "=IF(H" & i & "-F" & i & "<=0;0;H" & i & "-F" & i & ")"
with:
ws.Range("J" & i).Formula = "=IF(H" & i & "-F" & i & "<=0,0,H" & i & "-F" & i & ")"
If you want to make it easy, see:
Reference

Related

Can someone review my excel error and provide feedback on the code?

We have a large excel spreadsheet that has been in use for a view years now, the original coder for it has moved on. Today we started to get an a runtime error "Compile error, Syntax error" highlighting the below code
For n = 7 To lastpivotrow
If Range("AA" & n).Value <> "" And lastpivotrow < 50 Then
Range("B" & nextentry).Value = Range("AB" & n).Value
Range("C" & nextentry).Value = Range("AC" & n).Value
Range("D" & nextentry).Value = Range("AD" & n).Value
Range("E" & nextentry).Value = Range("AE" & n).Value
Range("B" & nextentry, "E" & nextentry).BorderAround LineStyle:=xlContinuous
Range("B" & nextentry, "E" & nextentry).Borders(xlInsideHorizontal).LineStyle = xlContinuous
Range("B" & nextentry, "E" & nextentry).Borders(xlInsideVertical).LineStyle = xlContinuous
Range("B" & nextentr+hen Range("B" & nextentry, "E" & nextentry).Interior.ColorIndex = 39
nextentry = nextentry + 1
Can anyone see whats wrong with this and provide feedback?

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

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

VBA Bloomberg Formula

I have a problem with my code, here it is:
Sheets("Data - Backtest").Cells(5, linCompteur - 1).FormulaR1C1 = _
"=BDH(R[-3]C[1],""PX_LAST"",""ED-"" & " & .Cells(6, 5).Value & " & ""AYA""," & .Cells(7, 5).Value & ", **""per="" & " & .Cells(9, 6).Value & "** , **""fx="" & " & .Cells(8, 5).Value & "** ,""Days=W"",""Fill=P"",""cols=2"")"
In the Excel it gives this,
=BDH(R[-3]C[1];"PX_LAST";"ED-" & 5 & "AYA";; **"per=" & d**; **"fx=" & EUR**;"Days=W";"Fill=P";"cols=2")
I see where is the problem, it's d and EUR, it should be this:
=BDH(R[-3]C[1];"PX_LAST";"ED-" & 5 & "AYA";;**"per=" & "d"**;**"fx=" & "EUR"**;"Days=W";"Fill=P";"cols=2";"cols=2;rows=1305")
but I don't know how to do this for the equivalent in VBA.
I brought some of the string concatenations together where there didn't seem any reason to separate them.
workSheets("Data - Backtest").Cells(5, linCompteur - 1).FormulaR1C1 = _
"=BDH(R[-3]C[1], ""PX_LAST"", ""ED-" & .Cells(6, 5).Value & "AYA"", " & .Cells(7, 5).Value & ", ""per=" & .Cells(9, 6).Value & """ , ""fx=" & .Cells(8, 5).Value & """ , ""Days=W"", ""Fill=P"", ""cols=2"")"
This is the result on the worksheet.
=BDH(R[-3]C[1], "PX_LAST", "ED-5AYA", , "per=d" , "fx=EUR" , "Days=W", "Fill=P", "cols=2")

VBA range.formula issue (LEN, RIGHT & LEFT)

I'm trying to do a VBA code to accomplish 2 things as follows:
Count how many characters there is on cell A1, using the formula LEN(A1) and one the last line, I'm trying to have the formula RIGHT(LEFT(A1;Q1-2);6) on cell J1
Please follow down my VBA code so far:
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("P" & i).Formula = "=LEN(" & cel & ")"
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-" & 2 & ")," & 6 & ")"
Next i
It seems something silly what is missing, however, I couldnt manage to solve it so far
Thanks in advance
You’re missing a Right, and some other things
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-2), 6)"

Excel Formula in vba?

I am trying to insert the following formula using vba:
Cells(i, 17).Formula = "=IF(""" & Range("M" & i).value & """ = """","""",IFERROR(INDEX(Contacts!$D:$D,MATCH(""*"" & """ & Range("M" & i).value & """ & ""*"",Contacts!$C:$C,0)),"""")"
For some reason i get an application undefined error. Please can someone show me where i am going wrong?
You are missing a )
Cells(i, 17).Formula = "=IF(""" & Range("M" & i).value & """ = """","""",IFERROR(INDEX(Contacts!$D:$D,MATCH(""*"" & """ & Range("M" & i).value & """ & ""*"",Contacts!$C:$C,0)),""""))"

Resources