Timer on Opening Workbook - VBA - vba

I created a macro that automatically opens a "raw" workbook file, does some calculations/manipulations, then saves it in another location. However, sometimes the original "raw" data file has corrupted characters that causes excel to get stuck while opening the file (even if done manually) -- it will just keep the opening file dialog open, with a full status bar, but will never actually proceed to opening the file or present an error. So, when we run the macro, it runs forever, trying to open the file but it is unable to proceed because the file never opens.
I am trying to add a timer to the function so that "if rawdata.xlsx takes longer than 5 minutes to open, cancel and stop procedure."
So far, I'm trying the following (file names have been changed for placeholders):
Sub wbprocess()
Dim Start, MaxTime, OpenStatus
OpenStatus = False
Start = Timer
MaxTime = 300 '(seconds)
Do While Timer < Start + MaxTime Or OpenStatus = False
Dim wb As Workbook
Set wb = Workbooks.Open(input file here)
' calculations and rest of code here
wb.SaveAs Filename:=output file here, FileFormat:=xlOpenXMLWorkbook
wb.Close
OpenStatus = True
Loop
End Sub
But, it won't leave the while loop until the max time has been reached, even if the file takes less than the allotted 5 minutes to open. What would be the correct logic to make it leave the while loop, and continue the calculations once the file has opened?

Related

Excel refuses to close when "Save when Close" Prompt is disabled

I have an Excel file, PlannerTool which keeps prompting to save the file when closing, even when it has been saved 2 seconds ago. This is probably due to some volatile code. So I thought I would disable the prompt when the file had been saved within a certain time window. However when I do so, Excel refuses to completely shutdown. It will close the workbook, but a grey screen will remain. See screenshot below.
The code I use to deny the prompt is taken from
VBA workbooks.Close without being prompted to if the user wants to save?
Complete code used provided below:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.EnableEvents = False
Dim SaveTime As Date
Dim CurrentTime As Date
Dim TimeDifference As Long
SaveTime = ThisWorkbook.BuiltinDocumentProperties("Last Save Time")
CurrentTime = Now
TimeDifference = DateDiff("s", SaveTime, CurrentTime)
'Gets the time difference between closing and saving in seconds
If TimeDifference <= 10 Then 'Saved less than 10 seconds ago so deny prompt
Application.DisplayAlerts = False 'Code gotten from StackOverflow Question
ThisWorkbook.SaveAs Filename:="PlannerTool", FileFormat:=xlOpenXMLWorkbookMacroEnabled, ConflictResolution:=xlLocalSessionChanges
ThisWorkbook.Saved = True 'Tweaked to fit saving format needs
ThisWorkbook.Close SaveChanges:=False
Application.DisplayAlerts = True
End If
Application.EnableEvents = True
End Sub
If the VB Editor is open when closing the workbook, the editor will remain open and display the RibbonX_Code Module. The VBA project containing the workbook PlannerTool and code are no longer displayed in the Project Tree, indicating that they are indeed closed. No difference when I have 1 or multiple workbooks opened, these stay displayed in the tree when I close PlannerTool. Can anyone tell me why Excel won't close properly?
I can replicate this behavior locally, and it appears that calling ThisWorkbook.Close is the issue. If you open a workbook and then use the Excel menu to close it, (File -> Close), it does the same thing as your code. In the "manual" case, Excel holds the application open because you opted not to close Excel - just the open workbook.
This should work as intended if you manually check to see if Excel should be closed.
If TimeDifference <= 10 Then 'Saved less than 10 seconds ago so deny prompt
Application.DisplayAlerts = False 'Code gotten from StackOverflow Question
ThisWorkbook.SaveAs Filename:="PlannerTool", FileFormat:=xlOpenXMLWorkbookMacroEnabled, ConflictResolution:=xlLocalSessionChanges
ThisWorkbook.Saved = True 'Tweaked to fit saving format needs
ThisWorkbook.Close SaveChanges:=False
Application.DisplayAlerts = True
If Application.Workbooks.Count = 0 Then
Application.Quit
End If
End If
I'd suggest this rather than forcing the application itself to close, as the user may have other documents open in Excel.

Shared Excel Document

I've got a Excel documents with macros with input and update forms for information, the document is used as a monthly database where you can run reports from as well.
The issue is that when using the document sometimes the input and update options are used at the same time causing information loss. Both the input and output save at the end of the macro to minimise the losses, but I was wondering if there is anyway of checking at runtime if there is a macro being use by another user and if so delay the next macro run until the other user is finished?
There is one way I can think of. Logically it should work. However I have not tested it.
Create a temp sheet and hide it
When anyone runs a macro, check if cell A1 of that sheet is empty or not
If it is empty then run the macro
Before running the macro, write to that cell and once the macro is run, clear the contents of the other cell
Sandwich your macro code as mentioned below
Code
Sub Sample()
Dim ws As Worksheet
ThisWorkbook.Save
Doevents
Set ws = ThisWorkbook.Sheets("HiddenSheetName")
If Len(Trim(ws.Range("A1").Value)) = 0 Then
ws.Range("A1").Value = "Macro Starts"
ThisWorkbook.Save
Doevents
'
'~~> Rest of your code goes here
'
ws.Range("A1").ClearContents
ThisWorkbook.Save
Doevents
Else
MsgBox "Please try after some time. There is a macro running... Blah Blah"
End If
End Sub
CAUTION: Once the code runs, you cannot undo the changes since the code save the file programatically. The changes are permanent. In a file which is not shared, you can undo by closing the file without saving and re-opening it.

Deleting hyperlinks performance

I want to delete all hyperlinks on currently active sheet via VBA.
For that I am using ActiveSheet.Hyperlinks.Delete command, which works fine and does not take virtually any time...
All that until I have opened two workbooks containing hyperlinks at the same time. In that case, the very same command takes much more time (minutes) to finish. It does its job, removing hyperlinks from the activesheet only, but in longer time. It seems somehow the other worksheets with hyperlinks is slowing it down.
I can have multiple workbooks opened at the same time, but they must not have any hyperlinks for the macro to work fast.
Can someone help me to overcome this?
I actually am in a situation where I frequently need to have both hyperlinks workbooks opened at the same time and running the macro which deletes hyperlinks.
This may not be ideal, but I'd consider starting your code with a check to see if other workbooks are open in the instance and if they are save the activeworkbook, open an new instance of excel and reopen the workbook in the new instance. Then run your code again.
Something like this to open the new instance:
Sub BlahBlah
if morethan1 then
CWb = ActiveWorkbook.name
ActiveWorkbook.save
Dim objXL
Set objXL = CreateObject("Excel.Application")
objXL.Visible = True
application.displayalerts = false
objXL.Workbooks.Open = CWb
application.displayalerts = True
end if
End Sub
If you isolate the workbook you should return to your normal runtime

Excel workbook only shows after running code is finished

I have a program in an Excel workbook that opens a Powerpoint-File, updates the links within this file and closes it after that. This works fine. Here is my problem: When the links are updated an Excel file with the source data is opened. After Powerpoint is closed this file stays open. I want it to get closed because I repeat this process for many files and I can't end up with hundreds of open Excel files.
I tried the following:
WBKs=Application.Workbooks.count
For i = WBKs to 1 Step -1
If Workbooks(i).Name<>ThisWorkbook.Name then
Workbooks(i).close savechanges:=False
End if
Next i
Now comes the weird part. Whenever I just run my code, WBKs always returns 1 and the Excel file only pops up after the code is finished. If I go through my code in debug mode it works. The workbook pops up as soon as I enter debug mode.
I tried Applicatio.Wait in the hope that the file would show after a second. The file only showed after the code was finished.
I tried a Do While Loop to wait until the file is open. Excel crashes because I never leave the loop.
Edit: I tried DoEvents as suggested. Does not work either.
This is just a workaround, but try using a brute force after x times your macro has run. Store that x somewhere in workbook, save. And kill excel process (all instances, including self) :
Dim sKill As String
sKill = "TASKKILL /F /IM excel.exe"
Shell sKill, vbHide
Found here : VBA script to close every instance of Excel except itself
When running your macro next time, you will use that x as a starting point for next PPT file to update.

Cancel External Query in Excel VBA

I have created an Excel Spreadsheet which helps with data analysis from an Oracle database.
The user enters then clicks the "Refresh Query" button which generates a query for Oracle to execute. The query takes a minute or so to complete. Although the VBA code does not hang on ".Refresh", all Excel windows remain frozen until the query completes.
Sub refreshQuery_click()
Dim queryStr as String
' Validate parameters and generate query
' ** Code not included **
'
' Refresh Query
With ActiveWorkbook.Connections("Connection").OLEDBConnection
.CommandText = queryStr
.Refresh
End With
End Sub
Is there a way for the user to manually cancel the query (calling .CancelRefresh) while the Excel user-interface is frozen?
EDIT I don't know if the following is worth noting or regular behavior. While the query is executing, all open Excel windows (including the VBA Editor) become "Not Responding" in Task Manager. Neither pressing Esc nor Ctrl+Break will cancel the script. Also, calling DoEvents (either before or after .Refresh) does not change this behavior.
Here's a method that I know will work. However, there are some complications.
Here's how it's done:
Put the spreadsheet with the data in a separate workbook. This worksheet should execute the refresh query when it's opened and then close once the data is updated.
Create a batch file to call the "Data" Excel file.
Within a different workbook, create a procedure (macro) for the user to call. This procedure will call the batch file, which subsequently calls the Excel file. Since you are calling a batch file and not Excel directly, the Excel procedure will continue because the command shell is released so quickly and opens the other Excel file in a different thread. This allows you to continue working within the main Excel file.
Here are some complications:
I included a method to alert the user that the data has been udpated. There are timing issues where it's possible to try to check if the data has been update when the workbook is not accessible, which forces the user to try to update values. I included a method called my time which pauses the execution of the code so it only checks every so many seconds.
The updated worksheet will pop up in a new window, so the user will need to click on their original worksheet and keep working. You could learn to hide this if you're comfortable with Windows scripting (I haven't learned that yet).
Here are some files and code. Be sure to read the comments in the code for why some things are there.
FILE: C:\DataUpdate.xls
We'll make a workbook called "DataUpdate.xls" and put it in our C:\ folder. In cell A1 of Sheet1, we'll add our QueryTable which grabs external data.
Option Explicit
Sub UpdateTable()
Dim ws As Worksheet
Dim qt As QueryTable
Set ws = Worksheets("Sheet1")
Set qt = ws.Range("A1").QueryTable
qt.Refresh BackgroundQuery:=False
End Sub
Sub OnWorkbookOpen()
Dim wb As Workbook
Set wb = ActiveWorkbook
'I put this If statement in so I can change the file's
'name and then edit the file without code
'running. You may find a better way to do this.
If ActiveWorkbook.Name = "DataUpdate.xls" Then
UpdateTable
'I update a cell in a different sheet once the update is completed.
'I'll check this cell from the "user workbook" to see when the data's been updated.
Sheets("Sheet2").Range("A1").Value = "Update Table Completed " & Now()
wb.Save
Application.Quit
End If
End Sub
In the ThisWorkbook object in Excel, there's a procedure called Workbook_Open(). It should look like the following so it executes the update code when it is opened.
Private Sub Workbook_Open()
OnWorkbookOpen
End Sub
NOTE: I found a bug when this file closed if 1) you accessed the file from the command line or shell and 2) you have the Office Live Add-in installed. If you have the Office Live Add-in installed, it will throw an exception on exit.
FILE: C:\RunExcel.bat
Next, we're going to create a batch file that will open the Excel file we just made. The reason that call the Excel file from within the batch file and not directly from the other Excel file using Shell is because Shell will not continue until the other application closes (at least when using Excel.exe "c:\File.xls"). The batch file, however, runs its code and then immediately closes, thus allowing the original code that called it to continue. This is what will let your uses continue working in Excel.
All this file needs is:
cd "C:\Program Files\Microsoft Office\Office10\"
Excel.exe "C:\DataUpdate.xls"
If you're handy with Windows Scripting, you do fancy things like open the window in a hidden mode or pass a parameter of the file name or Excel location. I kept it simple with a batch file.
FILE: C:\UserWorkbook.xls
This is the file that the user will open to "do their work in." They'll call the code to update the other workbook from within this workbook and they'll still be able to work in this workbook while this one is updating.
You need a cell in this workbook where you'll check the "Update Table Completed" cell from the DataUpdate workbook. I chose cell G1 in Sheet1 for my example.
Add the following code to a VBA module in this workbook:
Option Explicit
Sub UpdateOtherWorkbook()
Dim strFilePath As String
Dim intOpenMode As Integer
Dim strCallPath As String
Dim strCellValue As String
Dim strCellFormula As String
Dim ws As Worksheet
Dim rng As Range
Set ws = Worksheets("Sheet1")
Set rng = ws.Range("G1")
strCellFormula = "='C:\[DataUpdate.xls]Sheet2'!A1"
'This makes sure the formula has the most recent "Updated" value
'from the data file.
rng.Formula = strCellFormula
strFilePath = "C:\RunExcel.bat"
intOpenMode = vbHide
'This will call the batch file that calls the Excel file.
'Since the batch file executes it's code and then closes,
'the Excel file will be able to keep running.
Shell strFilePath, intOpenMode
'This method, defined below, will alert the user with a
'message box once the update is complete. We know that
'the update is complete because the "Updated" value will
'have changed in the Data workbook.
AlertWhenChanged
End Sub
'
Sub AlertWhenChanged()
Dim strCellValue As String
Dim strUpdatedCellValue As String
Dim strCellFormula As String
Dim ws As Worksheet
Dim rng As Range
Set ws = Worksheets("Sheet1")
Set rng = ws.Range("G1")
strCellFormula = "='C:\[DataUpdate.xls]Sheet2'!A1"
strCellValue = rng.Value
strUpdatedCellValue = strCellValue
'This will check every 4 seconds to see if the Update value of the
'Data workbook has been changed. MyWait is included to make sure
'we don't try to access the Data file while it is inaccessible.
'During this entire process, the user is still able to work.
Do While strCellValue = strUpdatedCellValue
MyWait 2
rng.Formula = strCellFormula
MyWait 2
strUpdatedCellValue = rng.Value
DoEvents
Loop
MsgBox "Data Has Been Updated!"
End Sub
'
Sub MyWait(lngSeconds As Long)
Dim dtmNewTime As Date
dtmNewTime = DateAdd("s", lngSeconds, Now)
Do While Now < dtmNewTime
DoEvents
Loop
End Sub
As you can see, I constantly updated the formula in the "Listening Cell" to see when the other cell was updated. Once the data workbook has been updated, I'm not sure how you'd force an update in code without rewriting all the cells. Closing the workbook and reopening it should refresh the values, but I'm not sure of the best way to do it in code.
This whole process works because you're using a batch file to call Excel into a different thread from the original file. This allows you to work in the original file and still be alerted when the other file has been updated.
Good luck!
EDIT: Rather than include a more complete answer in this same answer, I've created a separate answer dedicated entirely to that solution. Check it out below (or above if it gets voted up)
Your users can break the VBA function by pressing Ctrl+Break on the keyboard. However, I've found that this can cause your functions to randomly break until each time any function is run. It goes away when the computer is restarted.
If you open this file in a new instance of Excel (meaning, go to Start > Programs and open Excel from there), I think that the only workbook that will be frozen will be the one executing the code. Other intances of Excel shouldn't be affected.
Lastly, you might research the DoEvents functions, which yields execution back to the Operating System so that it can process other events. I'm not sure if it would work in your case, but you could look into it. That way you can do other things while the process is being completed (It's kind of dangerous because the user can then change the state of your application while the process is working).
I believe I know a way that actually will work, but it's complicated and I don't have the code in front of me. It involves creating a separate instance of the Excel application in code and attaching a handler to the execution of that instance. You include the DoEvents part of the code in a loop that releases once the application closes. The other instantiated Excel application has the sole purpose of opening a file to execute a script and then close itself. I've done something like this before so I know that it works. I'll see if I can find the code tomorrow and add it.
Well, you could consider the old-fashion way -- split the query into smaller batches and use Do Events in between batches.
You could try XLLoop. This lets you write excel functions (UDfs) on an external server. It includes server implementations in many languages (eg. Java, Ruby, Python, PHP).
You could then connect to your oracle database (and potentially add a caching layer) and serve up the data to your spreadsheet that way.
The XLL also has a feature to popup a "busy" GUI that lets the user cancel the function call (which disconnects from the server).
BTW, I work on the project so let me know if you have any questions.