I've got a spreadsheet that imports some data from a website and then filters and sorts it into a list. It reimports the data every 5 minutes.
I also have a cell, cell O1 located on the worksheet called FinalDisplay, that uses the following formula to timestamp when the last update occurred:
=TEXT(NOW(),"hh:mm AM/PM")
Lastly, I have a macro, called FilterSort that filters the list and then sorts the remaining results. This macro works fine when I run it manually.
What I am trying to do is have the change event of cell O1 getting updated at the time of the reimport trigger the macro to run so that, when the data is imported, it is automatically re-filtered/sorted. When the re-import happens and the timestamp is updated, however, nothing else happens.
The problem I think is that I think the Now() function getting recalcuated may not be handled as a change event because when I just punch in a number and overwrite the formula, the macro fires and everything works.
I've read that Now() is (or can be) a "volatile function" which might be handled differently than what I am expecting. Am I correct in thinking that this is the problem? Is there a way to resolve it so that the macro is called when the timestamp cell is updated?
Related
I have a long chunk of code that handles date data from a hidden datasheet, however anytime I enter a new date trough a userform, it writes it into the cell in the database in a format that for some reason can't be handled error free by my code chunk.
The problem is I need a sub to change the format (which is easy) but it does not actually apply the formatting unless I manually go into the datasheet, enter the cell and press enter.
I have searched and could not find a solution on how to do this in VBA, obviously this code is supposed to run automatic flawlessly plenty of times and having to manually go in an enter the cell defeats the entire purpose of the programming.
Sub Testing123()
MsgBox "run"
Workbooks("Excel Stock System.xlsm").Worksheets("DataNews_Events").Columns(8).NumberFormat = "dd.mm.yyyy hh:mm"
'something magic that actually automatically applies the format
End Sub
I successfully used the code in in one of your answers to display the Last Saved Date in my excel project. The code that I used is:
Function LastModified() As Date
LastModified = ActiveWorkbook.BuiltinDocumentProperties("Last Save Time")
End Function
It worked like a charm for about a month. However, since that time, the save date fails to update automatically. I have to unprotect the sheet, run the macro manually (usually I just select the cell and hit 'ENTER'). I'm not aware of any structure type changes in the excel file that might account for this. Does anyone have any thoughts as to what could be preventing the code from running?
Try making your function Volatile:
Function LastModified() As Date
Application.Volatile
LastModified = ActiveWorkbook.BuiltinDocumentProperties("Last Save Time")
End Function
I am automating a sheet and within vba I call:
sheet_name.calculate
The problem is this doesn't appear to be updating some of my cells which depend on API calls to financial addins. They all still contain #VALUE! However, if I press "shift + f9" on the same sheet then all the cells containing #VALUE! suddenly refresh and contain correct numerical values.
Why isn't .calculate doing this?
(My calculation update settings are set to "manual" - this has been decided as I don't want automatic updates, only when my code calls .calculate)
Try
Application.CalculateFull
http://msdn.microsoft.com/en-us/library/office/ff194064.aspx
If that doesn't work maybe this will help - it ensures all dependencies are rebuilt
Application.CalculateFullRebuild
http://msdn.microsoft.com/en-us/library/office/ff822609.aspx
I have not personally discovered any difference between worksheets("SheetName").Calculate and shift-f9 - but it may be worth trying this:
Worksheets("SheetName").EnableCalculation=False
Worksheets("SheetName").EnableCalculation=True
Worksheets("SheetName").Calculate
This forces a full calculation of the worksheet as opposed to a recalculate.
I have an excel sheet that has dde links to real time market data. I use a timer to watch the dde prices every second. Then submit orders when certain conditions are met. I tried an infinite loop with DoEvent in the middle which works for 5 seconds then freezes the workbook.
Is there a way to respond to changes in dde updates? Change event doesn't detect them. It just detects when user makes manual change.
I was told that if I have conditional formatting there's a way to pick up that event. So I can create a cell formula to turn true when my condition is met and then conditional format that cell to some formatting when it's true and then pick up the format change event. Is that possible? If so how. Any suggestions would be appreciated.
To Clarify: I want to pick up an event IN VBA that would submit an order to trade a stock. The way i'm doing this right now is with a timer which loops over all the rows looking for true cell in the trigger column. Once found it shuts off the flag for that row (sets the true condition to false) and submits the order.
The problem is that one second is an eternity for fast moving stocks. So I need an event to be thrown in VBA when a cell in trigger column turns true so I can respond immediately and not wait for the second interval of the timer class.
As far as I know, you can't call on the timer with a value of less than a second. If I could use milliseconds my problem would be solved. I'd just loop over the list every 10 milliseconds.
As far as I know I can't create another thread in VBA. If I could I would make an infinite loop and put it to sleep after every iteration for 10 milliseconds or so.
As far as I know, I can't pull dde directly into VBA or even .net for that matter since MSDN says it's no longer supported.
I hope this clarifies. All suggestion are appreciated.
If you create a dummy function which has your DDE output cells as parameters then you should be able to respond to the Worksheet_Calculate event?
I'm guessing that might work, but I've no experience with DDE: the DDE update may even trigger the Calculate event directly.
If you are asking if you can use conditional formatting to trigger an event, yes that is possible. I am not familiar with the DDE model myself, however, and mixing conditional formatting to trigger an event from a data condition does seem like an extra step, as Stepan1010 notes.
You may want to consider the discussion in Mr Excel in this link, as the issue centers around changing a cell value based changes made to cells from a DDE connection: http://www.mrexcel.com/forum/showthread.php?176508-Comments-VBA-amp-Min-Max
You may also consider using a DoEvent with a loop set for a time period based on how long you will actually implement the macro, if that applies to your application. This SO article is focused on a status bar, but I think the same logic applies in terms of an event execution, say based on a conditional within a loop: Force a screen update in Excel VBA
Hopefully this is helpful to you =)
~JOL
Why don't you just recreate whatever conditional formatting logic you have in that cell into a seperate cell?
For example your conditional formatting logic might highlight a cell when it is above a certain number - you could just put that logic in another cell- eg. =if(A1>100,TRUE,FALSE)
So I guess my question is - Why pick up the format change event when you can just pick up the event itself?
Edit for your clarification:
If you want to run a macro continuously in VBA you don't need a timer - you can just do a continuous loop like this:
Sub macro1()
Dim i As Double
With Sheet1
Do
'.Cells(5, 4).Value = i
i = i + 1
.Cells(1, 1).Value = i
' you are going to want to comment this out if you want to don't need to do other things
DoEvents
If Sheets("Sheet1").Range("A2").Value = True Then
' put your code here.
End If
Loop
End With
End Sub
So I must still be having trouble understanding your situation.
I have an Excel add-in (Bloomberg) which streams real-time prices into a spreadsheet. I need to add a column which contains the last time a price was updated. I've tried to do this with the worksheet change event, but this doesn't seem to get fired by the changes (I assume something to do with it being a real-time update).
The requirement is the same as the question below, but the solutions do not apply since the events do not fire in this case.
automatically execute an Excel macro on a cell change
With help from another forum, I've found an easy way to do this.
First define a function as below in a VBA module accessible to the sheet:
Public Function GetTime(target As Double) As Double
GetTime = Now()
End Function
Then in the 'last updated' column, add a call to this function with the formula pointing to the cell you wish to monitor. Whenever the target cell is updated, even from a real-time update, the function will fire and cause the time to be updated.