Excel cell value editing when VBA program running - excel

When Excel VBA macro excecuting, can we change values of cell in excel in which program is running?
Editing option i want to enable when script/program is running

When your VBA consists of a lot of loops you can still do things in excel by adding:
DoEvents
Add this inside the loop(s), this will allow for other events (such as the user changing stuff) to happen while the program runs.
Do keep in mind the warnings others already gave you, you can seriously disrupt your VBA script, because the user could actually mess with all the excel spreadsheet content that you might interact with in your script.
A little "fun" example:
Option Explicit
Sub example_DoEvents()
Dim timer1 As Date
timer1 = Now()
Do While Now() < timer1 + 10 / 60 / 60 / 24 'only let it last for 10 seconds
DoEvents
ActiveSheet.Range("A1").Value2 = Selection.Address
Loop
MsgBox "END"
End Sub

Related

How to run a sub at set intervals in Excel VBA?

I have a UserForm in which I have to run a sub every 5 seconds. I know that I could use Application.Ontime Now + TimeValue("00:00:05"), "my_Procedure", but it seems to work only with macros. When I try it with a sub, I get the message:
Cannot run the macro ''xx\xx\WorkBook1.xlsm'!my_Procedure'. The macro may not be available in this workbook or all macros may be disabled.
I'm an absolute beginner in Excel and VBA. I googled a lot but all answers seem to show how to repeat a macro every x seconds. How can I repeat a sub every x seconds?

What is "normal" Excel behavior of macro called by Application.OnTime during cell edit?

I am a VBA beginner trying to determine the "normal" behavior of excel during a timed call to a macro using Application.OnTime. In my code (below) I am calling ActiveWorkbook.RefreshAll but I think this question may be relevant to the timed call to any macro. As a newbie, I want to understand the behavior and limits of VBA so that I can determine if VBA is the right tool or if I should use another solution in certain cases (perhaps python / openpyxl).
My set up: I am using Microsoft Excel 365 (annual subscription) and the Account page says I am using Version 2006 (Build 13001.20384 Click-to-Run). I have a single workbook containing a single Sheet1 which contains the formulas to import stock data using the built in feature under the Excel Data tab. I have several rows (one stock per row) of columnar data corresponding to the stock fields such as ticker symbol, price, etc.
To automatically refresh the data, I borrowed & modified code to refresh the data every minute. The code works fine. The "issue" I have is that between refreshes, if I start editing any cell on the sheet and continue to do so during the time when the next automatic call to the refresh should occur, it indefinitely delays the execution of said call until the moment I complete (or cancel) the cell edit. When it does the refresh upon my edit completion that refresh does seem to work correctly.
If I start a cell edit and do not complete it for several minutes then only a single refresh occurs when I complete (or cancel) the edit, even though several intervals may have passed (it does not makeup/catchup the missed/delayed calls).
This behavior is OK with me in this application, but I would like to understand if this is considered "normal" behavior, and if I should expect it to always behave this way. Perhaps this question extends beyond a refresh call but also to any timed call to any macro that may be delayed as a result of a cell edit in progress.
Here is my code.
This is in a file called VBA-Test-02-StockData.xlsm under ThisWorkbook (Code):
Private Sub Workbook_Activate()
'ActiveWindow.WindowState = xlMaximized
Application.Speech.Speak ("Activating stock workbook")
Call AutoRefresh
End Sub
This is in the same xlsm file under Module1 (Code):
Sub RefreshStockData()
Application.Speech.Speak ("Refreshing stock data")
ActiveWorkbook.RefreshAll
End Sub
Sub AutoRefresh()
Call RefreshStockData
Application.OnTime Now + TimeValue("00:01:00"), "AutoRefresh"
End Sub

How to refresh database data in Excel at regular intervals?

In an Excel spreadsheet/VBA script I'm making, I need to call data from a database, and refresh the values every 5 minutes. The program starts from the push of a button, and should run continuously until the user breaks the execution. I'm currently not sure how to make Excel/VBA 'wait' 5 minutes without pausing the spreadsheet and, ideally, without being computationally inefficient.
I've tried using the "Application.Wait" and "Sleep" functions, but both of those pause the spreadsheet during the 5 minute wait.
My current solution is to use a "While" loop with "DoEvents" inside it, as shown in the code below. This makes the program just run the "While" loop for 5 minutes, and it does not pause the spreadsheet thanks to "DoEvents". However, while the spreadsheet is usable, this is computationally inefficient, since the program execution isn't technically paused, it's just running the "While" loop continuously, and some of the slower computers that may end up using my program might be significantly slowed by this.
My current solution is as follows:
Sub MainProgram()
'dimension variables, open database connection, etc.
Do While 1 < 2 'ad infinitum
'get database data, write to spreadsheet, etc.
WasteTime()
Loop
End Sub
Sub WasteTime()
EndTime = Now + TimeSerial(0,5,0)
While Now < EndTime
DoEvents
Wend
End Sub
The problem with this, as mentioned above, is the computational inefficiency. CPU utilization is fairly high throughout the WasteTime loop. So I'm wondering, is there any way to pause the script without pausing the spreadsheet and without running the code continuously, thus burdening the CPU?
As BigBen mentions in comments Application.OnTime is the best option for this. It avoids the overhead you describe by scheduling a second macro to be called at a future time.
Below is an example. You can modify the wait time with the constant variable. These should be within the same Module (or change theCalculation macro to not be private).
Sub TheTimerMac()
'enter in timevalue (hour:Minute:Second)
Const DelayTime As String = "00:05:00"
Dim nextRunTime As Date
nextRunTime = Now + TimeValue(DelayTime)
'Schedules application to execute below macro at set time.
Application.OnTime nextRunTime, "TheCalculation"
End Sub
Private Sub TheCalculation()
'whatever you use for your calc here
Application.CalculateFull
'This will restart the timer portion.
Call TheTimerMac
End Sub

executing macros on time interval in ms-project

I'm trying to create a macro to save an ms-project file in a specific format to a given location at a set interval while the application is open. I've got it mostly working, except the executing on an interval part.
I've had a bit of a google around and when using excel or word you can use the Application.OnTime method & a time offset to call a sub at the required interval. Unfortunately it doesn't look like this method exists in MS-Project.
Is there an alternative method I can use here or should I abandon this idea?
I realize this is a late answer, but I have a solution I used for a similar problem. Just jump on the Project_Change event since it runs frequently, and compare against a variable.
Public lastSave As Date
Private Sub Project_Change(ByVal pj As Project)
If DateDiff("n", lastSave, Now()) > 1 Then ' Replace 1 with # of minutes between saves.
Application.FileSave
lastSave = Now()
End If
End Sub

Excel VBA: Printing x copies each with an increasing number in a cell

Is it possible to either:
Increase a number in a cell each time a page has been printed: e.g. print 20 pages with numbers from 1-20 in the cell
or if that isn't possible:
Run a For-loop, each increasing the number and each time prompting the printer to print.
I know this is a very basic question, but bear with me - It is the 1st time I'm trying my luck at Excel macros.
Cheers
This code can be put into 'ThisWorkbook' object to increment a cell value each time something from the workbook is printed.
Private Sub Workbook_BeforePrint(Cancel As Boolean)
ThisWorkbook.Sheets("Sheet1").Range("A1").Value = _
ThisWorkbook.Sheets("Sheet1").Range("A1").Value + 1
End Sub
It is possible to use a VBA Macro to populate and print a worksheet. For Excel 2010, use the Developer ribbon to Record Macro. It will record the steps you want in a Visual Basic macro, which you can then edit. You can change the macro to use a For-loop, changing the worksheet each time before you print.

Resources