Return Workbook to a visible state

julhs

Active Member
Joined
Dec 3, 2018
Messages
476
Office Version
  1. 2010
Platform
  1. Windows
I hope someone can help me, I’ve stuffed up!!!!!!!
I have/had the below code in the Primary workbook (in it’s “This Workbook” module) with the intention of auto open a secondary workbook in a “Mininized” state.
VBA Code:
Private Sub Workbook_Open()
Application.ScreenUpdating = False
Workbooks.Open "C:\Users\***\Documents\My Documents\****.xlsm", False
ActiveWindow.Visible = False
Application.ScreenUpdating = True
End Sub
Big problem now is that when I open the secondary workbook (by whatever means), it opens it to the image added.
Snip image
Blank Excel Window Capture.JPG
When secondary workbook opens; Project explorer lists all the sheets in the workbook, as you would expect.
But, I simply can’t view the workbook with all the sheets.

I pray this is fixable as I have committed the cardinal rule of NOT ensuring I have a resent backup copy!!!!!
 
Try assigning variable names to your Workbooks so that the Macro knows of which you are working. You should try to avoid using ActiveWorkbook when using more than one Workbook. See if the following code presents the results that you are looking for. I don't think that you can avoid the screen flicker since changing Windows state is an OS method and not an Application method. I may be wrong. This code would be in the Workbook_Open event of the "Primary" Workbook.
VBA Code:
Private Sub Workbook_Open()
Dim pWB As Workbook, sWB As Workbook
Application.ScreenUpdating = False
Set pWB = ThisWorkbook
With pWB.Windows(1)
    .WindowState = xlMaximized
End With
Set sWB = Workbooks.Open("C:\Users\[UserNameHere]\My Documents\[FileNameHere].xlsm") 'Change UserNameHere and FileNameHere to suit your needs
Set sWB = Workbooks.Add
With sWB.Windows(1)
    .WindowState = xlMinimized
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi Skyybot.
Fallen into the bad habit of just using “Active sheet” because 99.99% of time I am only ever ON and referring TO the “Active sheet”, exception being on this occasion.
I must change my practise!!! as I repeatedly see in my searches EXACTLY your comment about
Try assigning variable names to your Workbooks
I’m not able to test out what you have given me right now, leave it with me and I’ll get back to you tomorrow
Many thanks
 
Upvote 0
TaskBarCapture.JPGAfterCodeRunCapture.JPG


I have endeavoured to mock up images and used the snip tool to hopefully show you what your code is giving; on the Screen and in the Task bar.
Code seems to be opening the “Secondary” book in a “Minimized” state but “Visible” in/on top of the “Primary” book.
But it is also creating a new ”Blank” workbook “Book1”.
BUT that is all rectified by “Maximizing” the ”Blank” workbook “Book1” and then closing it.

But what you give me got me thinking!!
I had coded myself into a situation at the onset that I needed to get out of.
I realised I didn’t even need to use “Minimize” for what I was actually wanting to do (just thought that was what I needed to do!!)
Having utilised the basics of your code, I have the below that does EXACTLY what I intended.
Which is; on opening the “Primary” it loads the “Secondary” and then the “Primary” is set to be the “Active/Visible” sheet.
VBA Code:
Private Sub Workbook_Open()
Dim pWB As Workbook, sWB As Workbook
Application.ScreenUpdating = False
Workbooks.Open ("C:\Users\**** \Documents\My Documents\****.xlsm") 'Change UserNameHere and FileNameHere to suit your needs
Set pWB = ThisWorkbook
With pWB.Windows(1)
pWB.Activate
End With
Application.ScreenUpdating = True
End Sub
Although I have “Application.ScreenUpdating = False/True” in code above it doesn’t seem to be necessary to prevent flicker in this instance, but have included it as good practice.
As a side note; all previous screen flicker disappeared once I restarted computer.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top