VBA: Waiting for Bloomberg BDP calls to finish - vba

I have a script that imports some external data into the worksheet, which in turn affects some =BDP(...) formulas. Optimally, I'd like to do some checks on the BDP results immidiately after copying the data.
The Bloomberg Excel Add-in updates asynchronously - how do I wait for the results and then resume the script? It seems that the results are only imported after the VBA script finishes, no matter how long it runs.
Thanks in advance
Martin

I built something similar using BDH. I had to release control so that the Bloomberg add-in can go and collect the data, then resume my code. I was able to do this using Application.OnTime.
For example, if you have a 'CheckForData' function that affects the =BDP calls, and another function called 'ProcessData' that checks the results, make an aynchronous call to to 'ProcessData' within 'CheckForData', e.g.
Sub CheckForData
' Your code here
Application.OnTime Now + TimeValue("00:00:05"), "ProcessData"
End Sub
Then within 'ProcessData' perform your checks.

Related

Automatic refresh a file at specific time

I have a question about refreshing data in excel. How can I refresh an excel file, that is on the server (PLSQL query) at a specific time with the computer turned off?
Sub MyCode2()
code()
End Sub
Sub Workbook_Open()
Application.OnTime TimeValue("01:00:00"), Procedure:="MyCode2"
End Sub
I tried this and "Call", but it doesn't work.
If the system is turned off, you cannot open Excel or run a macro
Have you looked into PowerAutomate? That can do a lot of the same things as a macro (like refresh things) but on the cloud meaning no need for a PC to be on

Excel macro is not working

I had successfully created a macro, but had to keep recreating it for every Excel doc I wanted to use it in. Now it won't work for any of the workbooks.
Function GetURL(rng As Range) As String
On Error Resume Next
GetURL = rng.Hyperlinks(1).Address
End Function
What I would like to do is activate this macro across ALL workbooks, anytime I open Excel and use =GetURL(A1) I want it to get the HTTP link.
There are 2 approaches that I can think about to perform this:
1. Make an Add In that would contain your UDF and install properly
Official Documentantion
I think this is the best approach due to your statement
Quote: anytime I open Excel and use =GetURL(A1) I want it to get the HTTP link.>
2. You may use the UDF from the original workbook like so:
=Book1.xlsm!GetURL(A1)
As long as the workbook is opened

VBA powerpoint, run timed task/macro in background across presentation

I am trying to build a presentation with a counter/number in the footer which evolves with time (as a function of some external data set present in an Excel spreadsheet). I developed the VBA code for loading data from the spreadsheet, built a footer in the Slide Master and a macro for updating the counter in it (and even graph some data in the footer), but I am unable to build the mechanism for updating the counter - e.g. every 2 seconds - across the presentation (by that I mean that the update has to happen independently of the active/view slide). I've seen a few threads with similar topics [ref, ref], which were not really answered or active, hence my question: is there a way to build a background timer for launching macros in VBA/Powerpoint?
Thank you and best regards,
Cedric
Excel has an Application.OnTime event to handle this but I can't see one in Powerpoint. Not sure what your setup is but if you have an Excel sheet open as well you could use Application.OnTime from Excel to run the Macro in Powerpoint
Something like
Excel
Application.OnTime Now + TimeValue("00:00:15"), "my_Procedure"
Public Sub my_procedure()
Dim PPObj As Object
Set PPObj = CreateObject("PowerPoint.application")
PPObj.Run "myPPTFile.ppt!MyMacro"
End Sub

Timeout in Excel VBA

I was wondering if it's possible to create a VBA macro in Excel, which will save the file each X minutes. I've already figured how to initialize the macro after excel startup and I found on the google something like this should pause the macro
Application.Wait(Now + TimeValue("0:10:00"))
but this will also block the user input and he cannot make any changes during that time.
This is not a anti-crash protection , the reason why I need this, is that some users are forgetting to save the document regularly...
Thank you
Francis
The 2 examples of Simoco will work great, but if you want to prevent having to deal with unnecessary saves (especially if you're working on network files, or large files), you can do a check everytime there is a change in the worksheet.
Just use the Worksheet_Change function to do that, here is a possible pattern:
Private Sub Worksheet_Change(ByVal Target As Range)
If (Now > TimeStamp)
ThisWorkbook.SaveAs BlaBlaBlaBlaBla
TimeStamp = Now + TimeValue("0:10:00")
End If
End Sub
TimeStamp needs to be a global variable defined in each workbook.
Btw, make sure that saving your file every X minutes doesn't screw with the undo / redo function of excel. I remember I had unwanted behaviors in the past when using an auto-save function.
Other thought: Google document won't require this type of macro as there is no need to save.

Save undo stack during macro run

I am wondering if there is a way to save ability to undo actions after macro has been run.
I do not care about results of macro - just need to undo actions that were done by user before macro.
Background:
I have a macro on the worksheet_change event that logs who and when made the change on this worksheet. I do not want it to restrict user's ability to undo his/her actions.
There is no easy way to do this, but it's possible. The approach to this is to create three macros, and use some global variables to save state:
MyMacro
MyStateSavingMacro
MyStateRevertingMacro
E.g. My macro changes Cells in Range A1:A10 of the active sheet. So, whenever the code to run my macro is called, it executes
Sub MyMacro()
Call MyStateSavingMacro()
' Copies contents and formulae in range A1:A10 to a global data object
'... Code for MyMacro goes here
'
'................
Call Application.OnUndo("Undo MyMacro", "MyStateRevertingMacro")
'This puts MyStateRevertingMacro() in the Undo queue
'So pressing ctrl-Z invokes code in that procedure
End Sub
Sub MyStateSavingMacro()
' Code to copy into global data structures anything you might change
End Sub
Sub MyStateRevertingMacro
' Code to copy onto the spreadsheet the original state stored in the global variables
End Sub
So there it is. It's not pretty, but can be done.
Ref: http://msdn.microsoft.com/en-us/library/office/ff194135%28v=office.15%29.aspx
Edit:
To preserve the Undo queue prior to your MyMacro being run, the inelegant solution would be to create a chain of 4-5 MyStateRevertingMacro_1, _2, etc. where you can apply the information from your Worksheet_Change logging system and then chain-up the Application.OnUndo in each of those, so Application.OnUndo for each of those Reverting Macros would refer the previous state reversion code.
You can use the hidden mirror sheet to do this. Of course it will work if your worksheet is simple enough. You must decide which cells are editable and copy them TO mirror sheet, which are not editable and copy them FROM mirror sheet. And your macro should work only in the mirror sheet. That's it.
This one is a bit old now but in case anyone is still trying to get this to work - I just tried setting the undo stack to be able undo the formatting on a column that a macro had reformatted and noticed that by doing that the full undo command worked (before I added this bit the undo was unavailable) - does not matter what the custom undo code contains (in my case I had not even created the routine yet), the application undo works perfectly
Application.OnUndo "Undo Amount Format", "sUndo_Col2"