Hiding the excels working with macro - excel

I want to hide the excels while macros working on them. Application.visible is not working i don't know why. Is it possible for me to just write a code at the beginning so that until the end the excels won't be visible. Minimization is also works for me.
Thanks in advance

Are you calling it correctly? i.e.
Application.Visible = False
Alternatively you could 'freeze' the screen so you don't see anything whilst the macro is operating using:
Application.ScreenUpdating = False

I see Application.Visible is working fine for me.
I tried below code. I triggered below code on button click.
The Excel sheet is not displayed only excel icon was available on task bar.
Sub test()
Application.Visible = False
Cells(1, 1) = "Testing Visibility"
MsgBox "testing visibility"
ActiveWorkbook.Save
Application.Visible = True
End Sub

Related

How can I keep excel from "Not Responding", when running my refresh macro?

Through research, it seemed all I needed to do was use this? But my screen still bugs out while refreshing. I am just wanting the screen to not change or do anything at all when running this macro.
Application.ScreenUpdating = False
Here is just a small piece of my code that runs in this macro for reference:
Sub Refresh_Data()
Box.Show
Application.Cursor = xlWait
Application.ScreenUpdating = False
Workbooks("IOM Denial.xlsm").RefreshAll
Workbooks("IOM Denial.xlsm").Worksheets("Home").Activate
Application.Cursor = xlDefault
Application.ScreenUpdating = True
Unload Box
End Sub
You are showing the box in a modal form, Excel is waiting for a response from you before proceeding any further than Box.Show. If you close the Box using the little cross, your code should continue to run.
You can fix this in your code by changing the line to
Box.Show False '<-- runs non-modal
Information from Microsfot here: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/showmodal-property

How to show only the userform and completely hide the excel window?

I have created a userform to add data in order to generate a report and multiple users will have access to the file containing the userform.However my problem is that everytime i open the ".xlsm" file there are instances where the excel workbook in the background is visible.Also i'm having problems when the userform initiates.I looked for the solution to this on the internet and got a lot many ideas.However,i still did not get what i was looking for and thus am asking the question.
I have tried the following code;
Private Sub Workbook_Open()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.WindowState = xlMinimized
ActiveWindow.WindowState = xlMinimized
'Also tried the following
'1 'Application.Visible = False(Have error running the code if i use this.
It worked fine in excel 2007 but in 2013 and 2016 and higher versions the code won't work properly.No idea why.)
'2 ' With Application (This was basically to hide the visible excel application window behind the userform)`
' .WindowState = xlNormal
' .Height = 500
' .Width = 300
' End With
'do stuff (Cells.ClearContents)
userform1.show
End Sub
I also tried creating and running a ".vbs" file but, it seems to avoid only the excel startup splash screen.
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = false
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\filepath\filename.xlsm")
Following are a few screenshots that might help you understand the problem better.
1: On startup the userform always opens up behind my current window.Is there a way to fix this?
2: Because of the first problem the user usually tends to click on the excel application icon on the task bar.
3: Thus making the workbook/worksheet visible in the background like so.I want to avoid this from happening.No matter what i want the user to see only the userform .Is there something i'm doing wrong?
PS. 1: I also saw that one can put a background image of one's choice to avoid showing the excel worksheet.But since i'm fairly new to vba i really don't know if that would be the best option and haven't tried it yet.I'd prefer tweaking the current code i have (if possible) to get the desired result.
In Excel 2019, the following worked for me:
Attached the following to the Workbook_Open event:
Private Sub Workbook_Open()
Application.Visible = False
UserForm1.Show
End Sub
Created a UserForm with a CommandButton where I did put the following:
Private Sub CommandButton1_Click()
Application.Visible = True
Unload Me
End Sub
Obviously you can use these codes at many more places, but this was just for test purposes:
Edit:
If this does not automatically bring Excel to the forefront, you might want to consider using:
AppActivate Application.Caption
Put that as a first line. Testing this threw an "error 5" to me but looking around the net this is definately not the case to all users. While this is a rare occurence, you can tackle that problem (if you have it) by implementing a waiting time (as that helped me):
Dim HoldOn As Date
HoldOn = DateAdd("s", 10, Now())
Do While Now < HoldOn
Loop
Lower the waiting time if you can as 10 seconds might be a bit much. The whole thing then looks like:
Private Sub Workbook_Open()
Dim HoldOn As Date
HoldOn = DateAdd("s", 10, Now())
Do While Now < HoldOn
Loop
AppActivate Application.Caption
Application.Visible = False
UserForm1.Show
End Sub

Excel VBA application.visible immediately set back to True

I have set up a new, empty, modeless userform, to fix my problem with the least amount of code involved.
For when the workbook is opened, the following code is executed to hide Excel and show the userform. It's the only code for the workbook.
Private Sub Workbook_Open()
UserForm1.Show
If Application.Windows.Count <> 1 Then
Application.Windows("test.xlsm").Visible = False
Else
Application.Visible = False
End If
End Sub
I have an empty userform with one single button. The only code for this userform is:
Private Sub CommandButton1_Click()
Application.Windows("test.xlsm").Visible = True
Application.Visible = True
Unload Me
End Sub
The last thing is a button on the first worksheet, to start the same process as when the workbook is opened. Its code:
Sub Button1_Click()
UserForm1.Show
If Application.Windows.Count <> 1 Then
Application.Windows("test.xlsm").Visible = False
Else
Application.Visible = False
End If
End Sub
Now my problem:
When I open the workbook, the userform shows up, but excel and the active window stay visible as well. However, if I click the button on the worksheet, Excel, or the window, are hidden as they should. Also, Excel, not the userform, has focus after loading everything.
The first time I ran this, it worked fine. I suspect changing the ShowModal setting in the editor screwed it up somehow, but that's just me guessing. Anyway, it doesn't work anymore as intended, no matter the modal setting now.
If I just run
Application.Visible = False
instead of the "if"-clause, Excel still stays visible and of course so does the active window.
This is driving me nuts.
What am I missing?
Edit: Link to my test file: Test File on my Dropbox
Might have to start it twice, because when the macros are blocked at startup and only activated after excel has completely loaded, the code works as intended.
Edit: I was able to test this on an excel 2010 pc and there the problem doesn't exist. So it might have something to do with the way newer Office Apps handle stuff.
I found myself having the exact same problem - modeless form opened with Workbook_Open() event that's also supposed to hide excel app (working on Excel 2016, 32bit).
The reason why it's working with UserForm property ShowModal set to True is because: the execution is suspended - it's waiting for user to interact with the UserForm that was shown.
If we change ShowModal to False (or call UserForm.Show vbModeless) then the execution is never suspended and once we reach End Sub of our Workbook_Open(), Excel appears to set Application.Visible = True on its own.
Only solution I've found thus far is this one - basically you suspend the execution by adding an infinite loop so Excel only gets to end of this event once you get rid of (Unload/Hide) the form that was shown previously.
My version looks like this:
Private Sub Workbook_Open()
Dim App As Object
Set App = startMenu
App.Show vbModeless
While App.Visible
DoEvents
Wend
End Sub
Then just to make sure that Excel is closed once that modeless UserForm is closed I've added this:
Private Sub UserForm_Terminate()
CloseApp
End Sub
Public Sub CloseApp()
ThisWorkbook.Saved = True
If Not OtherWorkbooksOpen Then
Application.Quit
Else
ThisWorkbook.Close
End If
End Sub
Public Function OtherWorkbooksOpen()
If Application.Workbooks.Count > 1 Then
OtherWorkbooksOpen = True
Else
OtherWorkbooksOpen = False
End If
End Function
EDIT:
Solution without infinite loop - schedule hiding of Excel:
Private Sub Workbook_Open()
Dim App As Object
Set App = startMenu
App.Show
If Not OtherWorkbooksOpen Then
Application.OnTime Now + TimeValue("00:00:01"), "HideExcel"
End If
End Sub
I think the userform1.show needs to be called after the execution of if statement.
Private Sub Workbook_Open()
If Application.Windows.Count <> 1 Then
Application.Windows("test.xlsm").Visible = False
Else
Application.Visible = False
End If
UserForm1.Show
End Sub
Not an answer, but I can't post this as a comment. This works for me - the user form appears, the application is hidden. I used "<>2" as I have a personal workbook. Can you confirm what happens for you?
Private Sub Workbook_Open()
If Application.Windows.Count <> 2 Then
Application.Windows("test.xlsm").Visible = False
Else
Application.Visible = False
End If
UserForm1.Show False
End Sub
I had the same issue, but I notice the form loads before the excel file. So I put a redundancy calling the application.visible = false, in the form.
Basically after clicking anything in the form, it will call the application.visible = false and the excel window will hide.

VBA Textbox Autofilter hides everything

I implemented a google like search box into my spreadsheet, using an activex textbox.. However, in the beginning it worked fine, but now it hides the whole used range and i only see blank cells. This really needs to work 100%, but i can't figure out what's the problem.
The code is as follows:
Sub TextBox1_Change()
Application.ScreenUpdating = False
Range("$O$7").AutoFilter Field:=14, Criteria1:=TextBox1
End Sub
I'd appreciate your help with this issue.
I don't see any statement to clear previous filters, for example:
Worksheets(1).AutoFilterMode = False
Also, there should be Application.ScreenUpdating = True before End Sub to restore screen updating.
If TextBox1.vlaue = "" Then Application.Autofiltermode= False

Hiding the formula bar in Excel for a specific file

I would like to hide the formula bar in a specific Excel file and only in this file. I know we can do it with VBA (Application.DisplayFormulaBar = False) but I am wondering if there is another solution like editing the CustomUI file.
Usually I edit the CustomUI file for hiding ribbon, adding custom tabs, ... It would be nice if we can hide the formula bar in this way.
Any suggestions?
Thanks.
Short answer is: No, you cannot.
Unfortunately, you cannot hide it by editing the CustomUI file. The formula bar has to be hidden using VBA. That being said, you can just run the hide operation on the Workbook_open event.
Private Sub Workbook_Open()
Application.DisplayFormulaBar = False
End Sub
You can turn it on/off depending on the active sheet like so:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "Sheet1" Then
Application.DisplayFormulaBar = False
Else
Application.DisplayFormulaBar = True
End If
End Sub
I know it's a 2011 issue, but the solutions shown above don't seem to be the right ones for the problem in question.
If you modify using vba Aplication.whatelse this change applies to the entire excel app
To fix this instead of turning on/off when you open/close the document uses the sheet's activate/deactivate events
This way when your hidden document is turned on and when you turn off swatches.
This works perfect for me, to hide status bar and formula bar.
I leave you code to see how it works
Private Sub Workbook_Activate()
Application.DisplayStatusBar = False
Application.DisplayFormulaBar = False
End Sub
Private Sub Workbook_Deactivate()
Application.DisplayStatusBar = True
Application.DisplayFormulaBar = True
End Sub
You can accomplish this by using the workbook activate en deactivate events.
Just put Application.DisplayFormulaBar = False into the activate event and Application.DisplayFormulaBar = true in the deactivate event.
To avoid all opened Excel sheet formula bar hidden you can go for hiding the formula for a particular excel.
Sub test()
Sheet1.Unprotect "test"
Range("A1").FormulaHidden = True ' range which you want to hide formula.
'your code here
Sheet1.Protect "test"
End Sub

Resources