My excel file contains a lot of formulas and I therefore want it to set calculation mode to manual as soon as it is opened. Otherwise calculation starts automatically and I have to wait for hours. I found this page:
http://excel.tips.net/T001988_Forcing_Manual_Calculation_For_a_Workbook.html
which should be able to do the trick. However it's not working for my excel-file. It states that in the VBA-code, in the section "ThisWorkbook", the following code should be entered:
Private Sub Workbook_Open()
Application.Calculation = xlManual
Application.CalculateBeforeSave = False
End Sub
As pointed it out, it doesn't work in my case. Does someone have an alternative solution?
The best way around this would be to create an Excel called 'launcher.xlsm' in the same folder as the file you wish to open. In the 'launcher' file put the following code in the 'Workbook' object, but set the constant TargetWBName to be the name of the file you wish to open.
Private Const TargetWBName As String = "myworkbook.xlsx"
'// First, a function to tell us if the workbook is already open...
Function WorkbookOpen(WorkBookName As String) As Boolean
' returns TRUE if the workbook is open
WorkbookOpen = False
On Error GoTo WorkBookNotOpen
If Len(Application.Workbooks(WorkBookName).Name) > 0 Then
WorkbookOpen = True
Exit Function
End If
WorkBookNotOpen:
End Function
Private Sub Workbook_Open()
'Check if our target workbook is open
If WorkbookOpen(TargetWBName) = False Then
'set calculation to manual
Application.Calculation = xlCalculationManual
Workbooks.Open ThisWorkbook.Path & "\" & TargetWBName
DoEvents
Me.Close False
End If
End Sub
Set the constant 'TargetWBName' to be the name of the workbook that you wish to open.
This code will simply switch calculation to manual, then open the file. The launcher file will then automatically close itself.
*NOTE: If you do not wish to be prompted to 'Enable Content' every time you open this file (depending on your security settings) you should temporarily remove the 'me.close' to prevent it from closing itself, save the file and set it to be trusted, and then re-enable the 'me.close' call before saving again. Alternatively, you could just set the False to True after Me.Close
Related
I have a spreadsheet (let's name it Spr1) that I need to refresh periodically. I don't want to refresh it every time I open it, because it takes time.
I created another "launcher spreadsheet" (Spr2) to start Spr1 with macro in argument.
Spr2 is opening with:
Private Sub Workbook_Open()
Application.Visible = True
Application.Run "'\\path\Spr1.xlsm'!Refresh_data"
Workbooks("Spr1.xlsm").Close SaveChanges:=True
Application.Quit
End Sub
Macro in Spr1:
Sub Refresh_data()
ThisWorkbook.RefreshAll
End Sub
The first macro is not waiting for second one to finish the refresh. It is terminating Excel right after opening Spr1.
How can I wait to finish "Refresh_data"?
It depends on type of default you have set.
Find your Query Properties, and set 'Enable background refresh' to False (Microsoft use True as default).
Then in your code you need to call for RefreshAll and wait for the data to load with the DoEvents. If you want your update of data to run on open you can use this in 'ThisWorkbook' Object.
Private Sub Workbook_Open()
For Each q In ThisWorkbook.Connections
q.Refresh
DoEvents
Next
End Sub
If you like to launch your update from a VBScript you can call your macro without even se Excel doing the work in the background. Use Notepad and past this code in and save as MyStarter.vbs where you can start it with a double click.
Dim xlApp
Dim xlWkb
Dim MyParam
MyParam = InputBox("Input your Parameter:","Enter parameter to the service")
if MyParam <> false then
Set xlApp = CreateObject("excel.application")
Set xlWkb = xlApp.Workbooks.Open("\\path\Spr1.xlsm",true,true)
xlApp.Run "Spr1.xlsm!Refresh_data", CStr(MyParam)
Set xlWkb = Nothing
Set xlApp = Nothing
end if
Then you can use your input parameter sent to the instance of your workbook like this :
Sub Refresh_data(MsgFromVBScript As String)
MsgBox ("This is your parameter from VBScript:" & MsgFromVBScript)
ThisWorkbook.RefreshAll
End Sub
I have an group of 3 interconnected workbooks that pull data from each other. The way I have it pulling data is through Connections. All files are located in the same drive. What I am trying to do is to open all three files at the same time to make edits. However, once I open workbook "A", I can not properly open files "B" or "C" as it displays that the file is in use by another user and only gives the option to open as Read-Only.
Is there a work around to open all files at the same time in Write mode? I have tried including a ChangeFileAccess to Read but that does not work.
Thanks!
UPDATE:
I linked all files using Data > Connections. To further expand, these files open as UserForms on startup and I have added this code under "ThisWorkbook" to do that (open the UserForm automatically and hide the workbook). I am beginning to think that my problem of not being able to open more than one of this documents at the same time is because of this piece of code, but I am not sure what it might be or if there's a better way to open Userform & hide workbook. Any advice is appreciated. Below is the code I have under "ThisWorkbook":
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'important to reset this
Application.IgnoreRemoteRequests = False
End Sub
Private Sub Workbook_Open()
'need to use ontime to allow xl to initialise fully
Application.OnTime Now, "ThisWorkbook.OnlyOneOfMe"
End Sub
Private Sub OnlyOneOfMe()
Dim xlApp As Excel.Application
On Error GoTo BAD
With Application
If Me.ReadOnly Or .Workbooks.Count > 1 Then
Me.ChangeFileAccess Mode:=xlReadOnly
Set xlApp = New Excel.Application
xlApp.Visible = True
xlApp.Workbooks.Open (Me.FullName)
GoTo BAD
Else
'stop opening from explorer (but not from excel)
.Visible = False
.IgnoreRemoteRequests = True
UserForm1.Show
.Visible = True
'.Quit
End If
Exit Sub
End With
BAD: If Err Then MsgBox Err.Description, vbCritical, "ERROR"
Set xlApp = Nothing
Me.Close False
End Sub
I have inherited an excel project, which I’ve been tasked to automate and develop further.
Use case being
An order comes in
User opens the base (root) excel file
Types in the relevant info (customer, order, data, tasks, etc)
And saves the new task file in a folder with tasks in progress
Leaving the base (root) excel basic and ready for next order.
The new task file needs to have a certain file name structure, which has been sorted.
My problem comes in on ActiveWorkbook.SaveAs filename.
As I step through F5 the lines, it goes through all the steps, creates the filename, shows the Save As UI, correctly shows the right folder and suggested filename, and actually saves the file. As I come out of ErrHandler and the last Application.EnableEvents = yes - it crashes. It seems to either be closing the base file, opening the new, or transfer active workbook to the new save file.
Which is fine, if it could stop crashing.
If criticalInput then
Msgbox “U been bad, U Shall not PASS!”, vbCritical ‘Not actual text or box showing, just for demo.
Else
Dim fileSaveName As Variant
'### It Crashes after saving (possible at opening)
fileSaveName = Application.GetSaveAsFilename( _
InitialFileName:=tasksfolder & "\" & suggestFilename, _
fileFilter:="Excel-projectmappe med aktive makroer (*.xlsm), *.xlsm")
If fileSaveName <> False Then
'### here is where you would save your file
DoEvents ‘ Debugging research tells this should be here. Not that is changes anything.
ActiveWorkbook.SaveAs fileSaveName ‘ During dev this has been disabled.
End If ' SaveAs
End If ' criticalInput
ErrHandler:
'## Reset back to default
Application.DefaultFilePath = strDefault
Application.EnableEvents = True
End Sub
Debugging
I can save the file just nicely, if I disable SaveAs and work only on the base (root) excel file.
I can open the new task file and save it nicely. (Just being a bugger about overwriting, Y/N)
Debugging research said I needed a DoEvents prior to SaveAs
Debugging research also said only to have the most relevant references present (in my case unselect OLE Automation)
During debugging, I’ve also created, AfterSave() and Open(). They triggers normally during normal operation, but fails when SaveAs has crashed. Eg as I disable SaveAs or when I open the excel book normally.
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
On Error GoTo ErrHandler
If Success Then
Application.EnableEvents = False
'MsgBox "Success save", vbInformation ' for debugging purposes.
End If
ErrHandler:
Application.EnableEvents = True
End Sub
Private Sub Workbook_Open()
'MsgBox "I haz been opened, yehawww", vbInformation ' debugging
End Sub
I would like to run a particular piece of code if and only if the target file is closed OR was opened by a local VBA macro. If the file is opened by a different user on the network, the code should not run.
So far I have this test code:
Sub refreshAll()
Dim wbIsOpen As Boolean, wbIsOpenByMe As Boolean
Dim fileName As String, filePath As String
Dim testWb As Workbook
fileName = "test.xlsm"
If IsWorkBookOpen(ThisWorkbook.Path & "\" & fileName) Then wbIsOpen = True
On Error Resume Next
Set testWb = Workbooks(fileName)
If Not testWb Is Nothing Then wbIsOpenByMe = True
Err.Clear
On Error GoTo 0
If wbIsOpen = False Or wbIsOpenByMe = True Then
ThisWorkbook.Connections("testcon").OLEDBConnection.BackgroundQuery = False
ThisWorkbook.refreshAll
DoEvents
End If
End Sub
This workaround kind-of-sort-of works, with the exception that it can't tell whether the file was opened manually or by some programmatic agent. Is there a way to do that, or is there an easier way to do what I've tried to do in my code?
If you know the macro which could be opening the file, do you have authority over it? Perhaps just have that macro change some protected value in the workbook - ie: have it add that moment's date and time to a hidden shape's title. Then this macro could check to see if the date and time on the title matches the current date and time (within a processing time margin of error).
This could work something like:
Sub MacroWhichCouldHaveBeenUsedToOpenWorkbook()
'Other code
'Code to open target workbook
Workbooks("TargetWorkbook.xlsx").Sheets(1).Shapes(1).Title = Now()
End Sub
Sub RunIfAutoOpened()
If (Now + #0:00:05#) < Workbooks("TargetWorkbook.xlsx").Sheets(1).Shapes(1).Title Then
'Run desired code
End If
End Sub
I have 3 input boxes that when a document is selected it opens the document and fills in a select box. With Excel 2013 these windows are opened in front of the document and have to be minimized in order to select the next one. Is there a way to have it automatically open documents in the background?
Workbooks.Open (file_path)
If Application.Version >= 15# Then
ActiveWindow.WindowState = xlMinimized
End If
This what I currently have, however I would rather it open in the background in the first place over having to minimize it. I have tried to turn off screen updating for the part, however that did not work.
Application.ScreenUpdating = False
Workbooks.Open (file_path)
Application.ScreenUpdating = True
Does Application.ScreenUpdating affect WorkBooks.open in Excel 2013?
Application.ScreenUpdating will not work with excel 2013.
You may try creating function:
Sub myScreenUpdate(screenUpdateRequest As Boolean, previousScreenUpdate As Boolean)
If screenUpdateRequest Then
Application.ScreenUpdating = previousScreenUpdate
Else
previousScreenUpdate = Application.ScreenUpdating
Application.ScreenUpdating = False
End If
End Sub
call req:
Call myScreenUpdate(False, previousScreenUpdate)
Call myScreenUpdate(True, previousScreenUpdate)
Dim previousScreenUpdate as boolean
Call myScreenUpdate(False, previousScreenUpdate) 'to get the current setting into previousScreenUpdate
Call myScreenUpdate(True, false) 'to set updating to false
'do your heavy code here and when finished:
Call myScreenUpdate(True, previousScreenUpdate)