Using excel vba, how to write a formula in a cell - excel

In Excel, A1 is equal to =B1/ BDP(C11&”Corp”, “ds036”)
Which BDP(Corp, ds036) is a function and parameters from Bloomberg.
How to use Excel VBA for this formula?
I was trying different ways in VBA to solve my point. One of them is like the line below,
Cells(1,1)=“B1/ BDP(C11&”Corp”, “ds036”)”
An other way I tried, to simplify,
For i=1 to10
Cells(i,1)=“Cells(i,2)/ BDP(cells(i,3)&”Corp”, “ds036”)”
Next
Also, if it can access directly to BDP function. That will be perfect!

try:
Cells(1,1).Formula = "=B1/ BDP(C11&""Corp"", ""ds036"")"
Note:
I used a different flavor of double quote
I doubled-up on the double quotes

Does this do what you want?
Range("A1:A10").Formula = "=B1/BDP(C3&""Corp"",""ds036"")"
If you assign a formula to a multi-cell range, I think Excel will automatically adjust relative cell references for subsequent rows/columns - similar to CTRL+D or dragging down.
This means you don't need to loop through each row and re-construct the formula string for each loop iteration.

Related

How to Autofill Logical Formulas to a certain cell using VBA?

Two questions 1) Can I autofill logical formulas to a cell using VBA and how (there is something wrong with my code) and 2)Can I autofill it only when data is entered and how?
The formula I want to put works when I put into a a cell but not VBA:
=IF(ISNUMBER(SEARCH("$",$A2)),"Scanner 2",IF(ISNUMBER(SEARCH("#",$A2)),"Scanner 1","Error"))
For the VBA I also used Relative Coordinates, did not work:
Range("C2").Value="=IF(ISNUMBER(SEARCH('$',RC[-2])),'Scanner 2', IF(ISNUMBER(SEARCH('#',RC[-2])),'Scanner 1','Error'))"
Pictures that may help:
The problem is a combination of the use of single quotes + you didn't add as . Formula:
So:
Range("C2").Formula = "=IF(ISNUMBER(SEARCH(""$"",$A2)),""Scanner 2"",IF(ISNUMBER(SEARCH(""#"",$A2)),""Scanner 1"",""Error""))"
Should work
Also, I see in your code you are using .Select, there are many ways to avoid using this. Check this link.
The below example will do the same thing:
With ThisWorkbook.Sheets("Sheet1")
.Range("C2:C100").Formula = "=IF(ISNUMBER(SEARCH(""$"",$A2)),""Scanner 2"",IF(ISNUMBER(SEARCH(""#"",$A2)),""Scanner 1"",""Error""))"
End With
You'll notice Excel will auto-adapt the formula to the correct cell references.

Is it possible to external reference a path once?

So I've got two workbooks, where Workbook A pulls out different cells from different worksheets of Workbook B and adds them altogether. Problem is, it does this so much that it can end up causing a formula in excess of the 8000+ characters because of the filepath.
Is there a formula or method that would help shorten this? Perhaps a way where you only refer to the file path once and then every cell reference inside the formula knows it's to that same file path? The process of constantly repeating the filepath just to get the Worksheet & Cell Reference is what causes the current simple addition formula to balloon. I cannot use a SUM function it would capture other data not intended.
If you have a criteria for what values you would want to sum, you can use the SUMIF/S function to add these values considering your criteria.
Ex.: =SUMIF('D:\test\[test_file.xlsx]Sheet1'!A1:A200,">="&3,'D:\test\[test_file.xlsx]Sheet1'!A1:A200)
will add values equal to or more than 3 in range A1:A200 of Sheet1 in test_file.xlsx in D:\test\ directory.
Another method is typing in the source workbook directory in a cell and making use of an INDIRECT function to do your addition.
Ex.: If you enter 'D:\test\[test_file.xlsx]Sheet1'! as text in cell B1, you can add values of A2, A5 and A7 by typing in =sum(indirect(B1&"A2"),indirect(B1&"A5"),indirect(B1&"A7")) thus significantly reducing character length.
You can as well try to reference all relevant cells of the source workbook to another sheet in the destination workbook and make your addition from that "helper" sheet.
I'd prefer the SUMIF/S option and having a reliable criteria to work with.
What seems to have worked out for me is using SUMPRODUCT. It has cut down on the formula length considerably. As there is a standard criteria of which I wanted to multiply across different worksheets, Array one refers looks for matching criteria from the first worksheet and then uses Array 2 for each worksheet.

Runtime Error 1004 Copy long array formula in vba

I have the following array formula in cell B2 in my Excel spreadsheet:
={IF(COUNT(IF(ISNUMBER(A30:A1000);IF(C30:C1000>A30:A1000-1;A30:A1000)))>=COUNT(IF(ISNUMBER(A30:A1000);IF(B30:B1000>A30:A1000-1;A30:A1000)));COUNT(IF(ISNUMBER(A30:A1000);IF(C30:C1000>A30:A1000-1;A30:A1000)));IF(COUNT(IF(ISNUMBER(A30:A1000);IF(C30:C1000>A30:A1000-1;A30:A1000)))<=COUNT(IF(ISNUMBER(A30:A1000);IF(B30:B1000>A30:A1000-1;A30:A1000)));COUNT(IF(ISNUMBER(A30:A1000);IF(B30:B1000>A30:A1000-1;A30:A1000)));COUNT(IF(ISNUMBER(A30:A1000);IF(C30:C1000>A30:A1000-1;A30:A1000)))))}
Now I want to use the following VBA code to copy this code into cell A2:
Sheets("Sheet1").Range("A2").FormulaArray = Sheets("Sheet1").Range("B2").Formula
However, when I use this code I get runtime error 1004.
Do you have any idea how to solve this issue?
character limit of 255 when passing arrays...
https://support.microsoft.com/en-us/kb/213181
Set up named ranges for a30:A1000 of A, B30:B1000 of B, and c30:c1000 of D (C is reserved so you can't use it and CC will make this formula too long also) (ctrl+f3 to open the named range manager and then hit new button)
Then Change your formula to use the named ranges.
=IF(COUNT(IF(ISNUMBER(A),IF(D>A-1,A)))>=COUNT(IF(ISNUMBER(A),IF(B>A-1,A))),COUNT(IF(ISNUMBER(A),IF(D>A-1,A))),IF(COUNT(IF(ISNUMBER(A),IF(D>A-1,A)))<=COUNT(IF(ISNUMBER(A),IF(B>A-1,A))),COUNT(IF(ISNUMBER(A),IF(B>A-1,A))),COUNT(IF(ISNUMBER(A),IF(D>A-1,A)))))
Alternately you could set up custom VBA functions that would allow you to replace the repetitive code within your if statement with shorter strings of characters to bring you under the 255. But ultimately you need to get under 255. Once under 255 your code works fine, either with my changes above or with a shorter array formula. (note that I changed the semi-colons to commas so you might need to change them back if you use semi-colons)
I believe the issue lies with the references within the formula. As the cell B2 references A30:A1000 in the formula, when you apply this to the cell A2 it tries to move the reference one column to the left - which it can't. In order to bypass this you can make the references absolute:
={IF(COUNT(IF(ISNUMBER($A$30:$A$1000);IF($C$30:$C$1000>$A$30:$A$1000-1;$A$30:$A$1000)))>=COUNT(IF(ISNUMBER($A$30:$A$1000);IF($B$30:$B$1000>$A$30:$A$1000-1;$A$30:$A$1000)));COUNT(IF(ISNUMBER($A$30:$A$1000);IF($C$30:$C$1000>$A$30:$A$1000-1;$A$30:$A$1000)));IF(COUNT(IF(ISNUMBER($A$30:$A$1000);IF($C$30:$C$1000>$A$30:$A$1000-1;$A$30:$A$1000)))<=COUNT(IF(ISNUMBER($A$30:$A1000);IF(B30:B1000>A30:A$1000-1;$A$30:$A$1000)));COUNT(IF(ISNUMBER($A$30:$A$1000);IF($B$30:$B$1000>$A$30:$A$1000-1;$A$30:$A$1000)));COUNT(IF(ISNUMBER($A$30:$A$1000);IF($C$30:$C$1000>$A$30:$A$1000-1;$A$30:$A$1000)))))}

A formula to copy the values from a formula to another column

I have a column of values created from a formula, I know I can copy the values over to another column by using the clipboard. BUT...I want my spreadsheet to be automatic whilst avoiding the use of VBA coding, so it would be ideal if I could create a formula for the next column which copies the VALUES over to the next column. Maybe an INDEX/MATCH kind of method but one that copies ONLY the values rather than the formulas.
So in essence I want a formula to copy the values from a formula....
You can use =A4, in case A4 is having long formula
Use =concatenate(). Concatenate is generally used to combine the words of several cells into one, but if you only input one cell it will return that value. There are other methods, but I find this is the best because it is the only method that works when a formula, whose value you wish to return, is in a merged cell.
For such you must rely on VBA. You can't do it just with Excel functions.
you can use those functions together with iferror as a work around.
try =IFERROR(VALUE(A4),(CONCATENATE(A4)))
What about trying with VLOOKUP? The syntax is:
=VLOOKUP(cell you want to copy, range you want to copy, 1, FALSE).
It should do the trick.
Copy the cell. Paste special as link. Will update with original.
No formula though.

Trouble computing Excel array formulas programmatically

I am writing values in an excel sheet using a MATLAB program. I am also writing values in cells using formulas (for e.g. the MATLAB program writes =AVERAGE(A1:A10) to a cell and this gets converted to appropriate value (i.e. when I open the sheet, I don't see the above formula text, rather the value).
However I am having trouble writing array formulas (ones with curly braces around). Usually a user enters them by pressing Ctrl+Shift+Enter combination, curly braces appear and appropriate value is computed. However when I write these formulas enclosed in curly braces from MATLAB program the value is not computed, I simply see the formula text (with curly braces around it).
It seems I am not able to simulate Ctrl+Shift+Enter effect by simply writing the array formula to a cell.
Is there any solution to this?
Excel auto-converts values that start with = into .formulas.
To save an array formula, you need to write directly to the FormulaArray property of the range.
More Details:
If you're using a normal formula then you have two options in VBA. You can set the cell's value or formula properties like this:
Range("A3").value = "=Sum(A1:A2)"
Range("A3").Formula= "=Sum(A1:A2)"
When using array formulas, you cant use either of these approaches, you need to store it into a different area of the cell:
Range("A3").FormulaArray = "=Sum(A1:A2)"
When the user pushed Ctrl+Shift+Enter they are telling excel to send the .value of the cell into the .FormulaArray section of the cell.
Otherwise by default when excel sees the = sign i assigns the .value into the .formula
Anyways, this is all relative the the excel object model even if it's in VBA.
Now that I've gone into that detail I believe that matlab's xlswrite function only writes to the value of cells, and cant write to this sub-property, I may be wrong.
Something liek this may work in MATLAB (untested)
mlrange='A3'; % or similar
Excel = actxserver ('Excel.Application');
Excel.Workbooks.Open(filename);
TargetSheet = get(Excel.sheets,'item','A');
TargetSheet.Activate
ran = Excel.Activesheet.get('Range',mlrange);
ran.FormulaArray = '=Sum(A1:A2)'

Resources