How to "plant" bloomberg function BDP into cell VBA - excel

I have the following problem,
Say I have the following cell (very simplified):
CBA 4.5 01/22/2019, I want to use VBA to plant a BDP() function in the adjacent cell in order to find out what the ISIN is. Without excel I would use =BDP(A1 & " Corp"; "ID_ISIN")right?
I am attempting to insert this function in VBA, and it does not work:
TOMS.Cells(1, 2).Value.Formula = "=BDP(" & TOMS.Cells(1, 1).Value & " Corp, ID_ISIN)"
Any ideas?

You forgot the " inside the formula, which must be doubled in vba:
TOMS.Cells(1, 2).Formula = "=BDP(""" & TOMS.Cells(1, 1).Value & " Corp"", ""ID_ISIN"")"

Related

write dynamic formula in cell using vba

Range(A1).Value = "=SUM(Range(B1), Range(B1).Offset(xlToRight))"
I want to write a formula in a cell by using VBA, but I want to make it dynamic.
How can I write this code?
What I've tried is written above.
try this:
Range("A1").Formula = "=SUM(" & Range("B1").Value & "," & Range("B1").Offset(0, 1).Value & ")"

excel vlookup multiple rows into one cell

I would like to get the data from several rows from one sheet sheet1 into a single cell on another sheet sheet2 based on a lookup.
For example there is data on one sheet :
sheet1
And I would like to lookup data based on id and return all the concerning rows into one cell like this:
sheet2
Is that possible with an excel formula or is this only solvable with VBA?
Thank you for your help in advance.
I found a vba that came close to a solution but didn't work. I've looked at "index, match" functions "small" functions but could find a solution that puts data into a single cell...
This is the vba code I found that came close to solution:
'Function SingleCellExtract(Lookupvalue As String, LookupRange As Range, ColumnNumber As Integer)
Dim i As Long
Dim Result As String
For i = 1 To LookupRange.Columns(1).Cells.Count
If LookupRange.Cells(i, 1) = Lookupvalue Then
Result = Result & " " & LookupRange.Cells(i, ColumnNumber) & ","
End If
Next i
SingleCellExtract = Left(Result, Len(Result) – 1)
End Function'
the vba threw value or compile errors.. it looks like it only returns values from one vertical column
"Is that possible with an excel formula or is this only solvable with VBA?"
It sure is possible through formula, but you'll have to have access to the TEXTJOIN function:
Formula in H2:
=TEXTJOIN(CHAR(10),TRUE,IF($A$2:$A$11=G2,$B$2:$B$11&", "&$C$2:$C$11&", "&$D$2:$D$11&", "&$E$2:$E$11,""))
Note: It's an array formula and need to be confirmed through CtrlShiftEnter
Drag the formula down and make sure you got textwrap selected on column H.
No access to TEXTJOIN? You can always create your own, for example:
Function TEXTJOIN(rng As Range, id As Long) As String
For Each cl In rng
If cl.Value = id Then
If TEXTJOIN = "" Then
TEXTJOIN = cl.Offset(0, 1) & ", " & cl.Offset(0, 2) & ", " & cl.Offset(0, 3) & ", " & cl.Offset(0, 4)
Else
TEXTJOIN = TEXTJOIN & Chr(10) & cl.Offset(0, 1) & ", " & cl.Offset(0, 2) & ", " & cl.Offset(0, 3) & ", " & cl.Offset(0, 4)
End If
End If
Next cl
End Function
In cell H2 you can call the UDF through =TEXTJOINS($A$2:$A$11,G2) and drag down. Again, make sure textwrapping is checked for the column.
EDIT:
As per OP's comment, this is how I got the data to show correctly:
Select column H and click textwrap + top alignment as shown in this screenshot:
Next, select all cells if result is not correct yet:
Double-click the line between columns and rows to space them to fit the data

Error when trying to insert a SUMIF function using VBA

I am trying to insert a SUMIF formula into a cell using VBA. VBA is not allowing my code to run as it is. Any suggestions are appreciated.
ws and na are properly set earlier on in the code. If I simply change the SUMIF formula to a random value "x" , it appears in the desired cell. The error is occurring within the SUMIF formula that I am trying to insert into a cell.
ws.Range("B" & na.Row + 2).Value = "=SUMIF(OFFSET(B1,,,ROW()-1,1),"<>#N/A"))"
The purpose of this formula is to SUM a column of numbers while ignoring any cells that contain "#N/A".
When using quotes in a formula, you need to "double up":
ws.Range("B" & na.Row + 2).Formula = "=SUMIF(OFFSET(B1,,,ROW()-1,1),""<>#N/A"")"
You can use AGGREGATE and remove the OFFSET which is volatile
ws.Range("B" & na.Row + 2).Formula= "=AGGREGATE(9,6,B1:B" & na.Row + 1 & ")"
Try using 'Chr(34)':
ws.Range("B" & na.Row + 2).Formula = "=SUMIF(OFFSET(B1,,,ROW()-1,1)," & Chr(34) & "<>#N/A" & Chr(34) & ")"
Edit: Deleted quotes written by mistake

Application-defined or object-defined error when using .formula

I've been struggling to get a formula typed into a cell on a sheet. it's my activesheet (definitely).
Source is a file with full path. the sheet name is ... and lta_col_letter is the column letter I'm typing the formula into.
ActiveSheet.Range(lta_col_letter & 2).Formula = "=IFERROR(INDEX('[" & Source & "]...'!$G:$G,MATCH(" & versionref_col_letter & "2,'[" & Source & "]...'!$B:$B,0))," & Chr(34) & Chr(34) & ")"
I'm not sure why I'm getting this error. Help please!
EDIT:
Works when I added the missing [ brackets, but when it goes into the cell it adds the file name again after the sheet name and before the cell reference.It looks fine in a msgbox.
Looks like you are missing some opening square brackets around source.
ActiveSheet.Range(lta_col_letter & 2).Formula = "=IFERROR(INDEX('[" & source & "]...'!$G:$G,MATCH(" & versionref_col_letter & "2,'[" & source & "]...'!$B:$B,0)), text(,))"
To examine these types of errors, put a ' before the opening = and run the procedure to put the formula into the cell as a string. Go to the worksheet and perform a visual examination and try to remove the '. Make corrections until you have a working formula then transfer the corrections to the VBA formula string.
Since it seems that you have used an open workbook to retrieve the workbook name into source, you might consider using the Range.Address Property with External:=True to retrieve a fully punctuated external address.
dim sourceG as string, sourceB as string
sourceG = workbooks(source).worksheets("...").range("G:G").address(external:=true)
sourceB = workbooks(source).worksheets("...").range("B:B").address(external:=true)
ActiveSheet.Range(lta_col_letter & 2).Formula = _
"=IFERROR(INDEX(" & sourceG & ", MATCH(" & versionref_col_letter & "2, " & sourceB
& ",0)), text(,))"

VBA - VLookup function not woking

Can anyone tell me why is this not working? This stop working my macro and an error message appears
Sh.Cells(2, 13).Formula = WorksheetFunction.VLookup(Sh.Cells(2, 6) & " - " & Sh.Cells(2, 8), Worksheets("Licenciaturas").Range("H2:K2928"), 4, False)
Thanks!
Are you trying to add it as a formula to the cell, or just return the result of the VLookup and place that in the cell?
Your code looks like it's trying to do a bit of both.
If you want a formula to appear in your sheet use:
Sh.Cells(2, 13).FormulaR1C1 = "=VLOOKUP(R2C6 & "" - "" & R2C8, 'Licenciaturas'!R2C8:R2928C8,4,FALSE)"
The formula will appear as: =VLOOKUP($F$2 & " - " & $H$2, Licenciaturas!$H$2:$H$2928,4,FALSE)

Resources