Make window open in background - excel

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)

Related

Excel gets corrupt and goes into read only mode

So I have an excel document that will just randomly break on me when opening it. I do have Code 1 in the VBA ThisWorkbook section but it doesn't start until I enable macros after opening it.
The images attached are in the order that they appear to me. One note is that I do have hidden files revealed and I only see the "Ownership file" when I have it open. I am on a shared network but I do not have the privileges to view where it all is open.
My current work around is to save the file under a different name and then delete the old file and rename it.
After researching a bit, someone stated it might have had to do with sorting. But I added Code 2 and I am still having the issue.
Code 1
Dim Result
Result = MsgBox("The Data in this document might be outdated. Would you like to refresh the Data Queries? This process could take a few minutes...", vbYesNo, "Data Query OutDated")
If Result = vbNo Then
Exit Sub
End If
MsgBox "Queries Will Refresh Upon Closing this window. Please wait"
ActiveWorkbook.Worksheets("SQLData").EnableCalculation = False
ActiveWorkbook.Worksheets("FlowBreakDown").EnableCalculation = False
Application.ScreenUpdating = False
Change_Background_Refresh False
ActiveWorkbook.RefreshAll
Application.ScreenUpdating = True
ActiveWorkbook.Worksheets("SQLData").EnableCalculation = True
ActiveWorkbook.Worksheets("SQLData").EnableCalculation = False
ActiveWorkbook.Worksheets("FlowBreakDown").EnableCalculation = False
MsgBox "Refresh Complete"
Call ResizeData
End Sub
Code 2
Dim Sht As Worksheet
' Clear all Sort Fields prior to Save & Exit
For Each Sht In Application.Worksheets
Sht.Sort.SortFields.Clear
Next Sht
End Sub

Keeping Application.ScreenUpdating = False After Running Macro

I'm trying to create a hotkey that will toggle Application.ScreenUpdating. I just learned that the newer Excel versions automatically reset Application.ScreenUpdating to true after running a macro which is good 99.99% of the time except I don't want that to happen I want it to be persistent. Is there anyway for me to keep Application.ScreenUpdating = False?
Sub suspend_screen_update()
MsgBox "1st " & Application.ScreenUpdating
If Application.ScreenUpdating = True Then
Application.ScreenUpdating = False
Else
Application.ScreenUpdating = True
End If
MsgBox "2nd " & Application.ScreenUpdating
End Sub
Private Sub hk_toggle_screen_update()
Application.OnKey "{F1}", suspend_screen_update
End Sub
Why? My boss sometimes request that I only use Excel, nothing else, to manipulate a lot of data. Excel can be super slow if not crash when the data gets really big. I'm trying to save us time by creating this hotkey.

GetOpenFilename behaviour different for version earlier than Excel 2016

I have the following code:
Private Sub CommandButton3_Click()
Dim SPATH As String
Dim myFileName As Variant
If MsgBox("Some Question...", vbYesNo) = vbNo Then Exit Sub
'Used to open the VO2 report taken from the Metabolic Cart. Will close later
myFileName = Application.GetOpenFilename(FileFilter:="Excel Files,*.xls*")
If myFileName <> False Then
Application.ScreenUpdating = True
Workbooks.Open Filename:=myFileName
End If
MultiPage2.Value = 3
End Sub
It's job is to search for and open another Excel file while a userform is being run with the workbook it's currently tied to is visible=False with this:
Private Sub Workbook_Open()
Application.ScreenUpdating = False
ThisWorkbook.Application.Visible = False
UserForm2.Show
Windows(ThisWorkbook.Name).Visible = True
Application.Visible = True
Application.ScreenUpdating = True
End Sub
Also important note: Showmodal for this userform is True
When this is run using Excel 2016, it works no problem (i.e. I am able to use a refedit on the userform while the loaded Excel file is visible to use it on).
On my work computer which has an older version of Excel, the loaded Excel file is invisible (meaning I can't use the refedit I have on the userform).
Does anyone know what may be going on or have a possible solution?

Unable to open Excel from Explorer when userform is opened

Hi I am working on a project where I have to let users open excel while the Userform is opened.I can navigate through other excel files but not the one from Explorer.Please help.It would be of great help for me.
Option Explicit
Private Sub Workbook_Open()
Application.OnTime Now, "ThisWorkbook.OnlyOneOfMe"
Dim wks As Worksheet
For Each wks In ActiveWorkbook.Worksheets
wks.Protect Password:="Nothing", _
UserInterfaceOnly:=True
Next wks
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'important to reset this
Application.IgnoreRemoteRequests = False
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
If the UserForm is modal it will lock the instance of excel until the form is closed.
You need to make the UserForm non Modal or close it after the workbook is opened.
Or you may Disable events before opening the workbook, that will prevent the UserForm to popup.
Just put this before the line where you open the workbook
Application.EnableEvents = False
And after the opening line enable the evens again
Application.EnableEvents = True
And you need to Enable/Disable the events for correct instance/application since you are opening new.
like this:
XlApp.EnableEvents = False
XlApp.Workbooks.Open (Me.FullName)
XlApp.EnableEvents = True
But you probably wont need to open new excel instance if this would work.
Also put the events enabling line to the ErrorHandling
If Err Then Application.EnableEvents = True: MsgBox Err.Description, vbCritical, "ERROR"
You can also try to hide all opened UserForms.
Put this right after you open the workbook:
For Each Object In VBA.UserForms
Object.Hide
Next
I would rather use a visual basic script to open up the userform in your VBA project. Put the following code on a plain text file and save it with the '.vbs' extension.(This must be in the same folder as the excel file containing the userform)
Option Explicit
Dim fso, curDir
Dim xlObj, file
Dim fullPath
Const xlMaximized = -4137 'constant to maximizes the background excel window
On Error Resume next
Set fso = CreateObject("Scripting.FileSystemObject")
curDir = fso.GetAbsolutePathName(".")
file ="\~$YourExcelFileName.xlsm"
fullPath = curdir & File
If fso.FileExists(fullPath) Then ' checks if the project is open or not
MsgBox "The project is in use!",64, "Notificación"
Else
file ="\YourExcelFileName.xlsm"
fullPath = curdir & file
Set fso = Nothing
Set xlObj = CreateObject("Excel.Application")
With xlObj
.WindowState = xlMaximized
.Visible = False
.Workbooks.Open fullPath
.IgnoreRemoteRequests = True
.Run "mainMethod"
End With
set xlObj=Nothing
End If
... then add a public subroutine in your vba project to listen the call from the previous VBScript (name the subroutine as above, I've called mainMethod)
Public Sub mainMethod()
UserForm1.Show vbModeless
End Sub
... you also have to attach an userform_terminate event to indicate that when you close the userform it must quit the current and active instance of excel:
Private Sub UserForm_Terminate()
Application.Quit
End Sub
... and of course you have to write a workbook's before_close event to reset the .IgnoreRemoteRequests to false, as follow (You can also write this in the previous userform_terminate event handler, but I believe this a tidier way to do it):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Saved = True 'if needed
Application.IgnoreRemoteRequests = False
End Sub
Once you manage to do that, you'll have a very clean stand alone application, no one will notice it comes from an excel file and it won't interfeers with any other excel instance. Good Luck.
Andrés Alejandro García Hurtado

How to set calculation mode to manual when opening an excel file?

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

Resources