VBA Bloomberg Formula - excel

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

Related

VBA subtracting two dates and creating a new column

I have a function, I would like it to subtract dates from me and create a new column. However, it does not work, it shows me an error here
.Value = Evaluate("if({1}," & .Offset(, -2).Address & "-" & .Offset(, -1).Address & ","""")")
Set shtResult = Sheets.Add.Name = "NPE"
But not help. Below is my code :
With wbMe.Sheets("NPE").Range("G2", Range("A" & Rows.Count).End(xlUp).Offset(, 1))
.Value = Evaluate("if({1}," & .Offset(, -2).Address & "-" & .Offset(, -1).Address & ","""")")
End With
Solution for above:
With wbMe.Sheets("Arkusz3").Range("G2:G" & Range("A" & Rows.Count).End(xlUp).Row)
.Value = .worksheet.Evaluate("if({1}," & .Offset(, -3).Address & "-" & .Offset(, -2).Address & ","""")")
End With

.Formula brings error with punctuation marks vba [duplicate]

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

VBA Code adding incorrect column rows for sub-totals

I am trying to update a previous employees code.
Column D and E are not adding the correct sub-totals. It seems for each sub total row, it is counting A4 which is the first row of numbers.
Not sure how to adjust the code.
Set firstSub = Range("D" & cTL.Row) 'set first sum from
For Each c In Range("D" & cTL.Row, "D" & cBR.Row)
If c.Value2 = "" Then
c.ClearContents
End If
'This if will only run for column D, but will fill column D and E with total fields
If Right(c.Offset(0, -2).Value2, Len(sTotal)) = sTotal Then
c.FormulaR1C1 = "=sum(R" & firstSub.Row & "C" & c.Column & ":R" & c.Offset(-1, 0).Row & "C" & c.Column & ")"
c.Offset(0, 1).FormulaR1C1 = "=sum(R" & firstSub.Row & "C" & c.Offset(0, 1).Column & ":R" & c.Offset(-1, 0).Row & "C" & c.Offset(0, 1).Column & ")"
formulaStrD = formulaStrD & c.Address([], [], xlR1C1) & ","
formulaStrE = formulaStrD & c.Offset(0, 1).Address([], [], xlR1C1) & ","
ElseIf Right(Range("A" & c.Row), Len(sTotal)) = sTotal Then
formulaStrD = Left(formulaStrD, Len(formulaStrD) - 1)
formulaStrE = Left(formulaStrE, Len(formulaStrE) - 1)
c.FormulaR1C1 = "=SUM(" & formulaStrD & ")"
c.Offset(0, 1).FormulaR1C1 = "=SUM(" & formulaStrE & ")"
End If
Next c
For Each c In Range("E" & cTL.Row, "H" & cBR.Row)
If c.Value2 = "" Then
c.ClearContents
End If
Next c
End Function
The key to fixing this (I think) is to 'reset' the "first row" each time the value in column B changes - otherwise, each sub-total for each distinct value in column B will reflect the aggregate of all rows above it - including the other sub-totals.
Set firstSub = Range("D" & cTL.Row) 'set first sum from
For Each c In Range("D" & cTL.Row, "D" & cBR.Row)
If c.Value2 = "" Then
c.ClearContents
End If
'This if will only run for column D, but will fill column D and E with total fields
If Right(c.Offset(0, -2).Value2, Len(sTotal)) = sTotal Then
c.FormulaR1C1 = "=sum(R" & firstSub.Row & "C" & c.Column & ":R" & c.Offset(-1, 0).Row & "C" & c.Column & ")"
c.Offset(0, 1).FormulaR1C1 = "=sum(R" & firstSub.Row & "C" & c.Offset(0, 1).Column & ":R" & c.Offset(-1, 0).Row & "C" & c.Offset(0, 1).Column & ")"
formulaStrD = formulaStrD & c.Address([], [], xlR1C1) & ","
' Fix the Column E subtotal reference
formulaStrE = formulaStrE & c.Offset(0, 1).Address([], [], xlR1C1) & ","
' Reset the "firstRow" so that we don't accidentally pickup
' the other subtotals
Set firstSub = c.Offset(1, 0)
ElseIf Right(Range("A" & c.Row), Len(sTotal)) = sTotal Then
formulaStrD = Left(formulaStrD, Len(formulaStrD) - 1)
formulaStrE = Left(formulaStrE, Len(formulaStrE) - 1)
c.FormulaR1C1 = "=SUM(" & formulaStrD & ")"
c.Offset(0, 1).FormulaR1C1 = "=SUM(" & formulaStrE & ")"
' Reset the subtotal formulas along with the "firstRow"
formulaStrD = ""
formulaStrD = ""
Set firstSub = c.Offset(1, 0)
End If
Next c
For Each c In Range("E" & cTL.Row, "H" & cBR.Row)
If c.Value2 = "" Then
c.ClearContents
End If
Next c

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

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