Link to home
Start Free TrialLog in
Avatar of Bernard Thouin
Bernard ThouinFlag for Switzerland

asked on

Problem setting a text with VBA in Excel cells

Hi

I'm trying to "prepare" an Excel (2007 SP3) spreadsheet to make calls to Bloomberg functions to get some stock exchange data automatically. To do that, I have to insert in a number of cells a call to a Bloomberg function, which is not valid on my PC, because the spreadsheet is going to run and get its data on a separate Bloomberg-equipped, highly restricted machine, and the called Bloomberg function is only available on that machine. However, I'd like the user who is going to "run" the Excel to only have an absolute minimum of handling to do, hence my goal to give him a ready-to-run Excel.

What I want to insert in some part of the Excel is, for a given cell, something like this:

=BDP($B2;C$1)

I build this string with VBA code, but when I say:

Cell(x,y) = sTemp            ' sTemp is the string holding the text "=BDP($B2;C$1)"

I get an error ("Application-defined or object-defined error"), presumably because the formula is not valid, as the BDP function is not defined on my machine. So I added an error ignoring statement:

myrange.Errors.Item(xlInconsistentFormula).Ignore = True           'myrange is the cell area

but that also generates an error, actually the same error as on the cell setting statement.

How can I still insert such an invalid formula in my Excel ? Is that just impossible, i.e. should that part of the VBA code run only when the spreadsheet is on the "Bloomberg machine" ? Or am I missing something ?

And why is the error-ignoring statement generating an error ?

Thanks for help.
Bernard
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bernard,
I believe that Rgonzo1971 is correct--VBA expects to use American type formulas with a comma as list separator even though your computer in Prague uses a semicolon.

Try the following sub to see if it correctly installs the formula at your site. It turns off error handling while putting the formula in cell C2. It also shows how you might use R1C1 addressing to put a similar formula in a range of cells.
Sub BloombergInstaller()
Application.DisplayAlerts = False
'Cells(2, 3).Formula = "=BDP($B2,C$1)"          'Even if semicolon is list separator, must use comma when using VBA
Range("C2:F5").FormulaR1C1 = "=BDP(RC2,R1C)"    'Even if semicolon is list separator, must use comma when using VBA
Application.DisplayAlerts = True
End Sub

Open in new window

Brad
Avatar of Bernard Thouin

ASKER

Hi Rgonzo1971

Spot on ! I would never have thought that using a comma in the VBA code would then generate a semicolon in the Excel sheet, but it does... Amazing, what these stupid problems with separators can cost us in terms of lost time (and patience) !

Thanks a lot
Bernard