Solved

Prevent VBA written for one Excel workbook from affecting other workbooks

Posted on 2013-12-29
5
3,023 Views
Last Modified: 2014-01-18
Hello,

With multiple workbooks open in Excel 2013, is there a way to prevent VBA code which applies to only one of the workbooks from acting on the other open workbooks?

For example, quite some time ago, an EE expert (dlmille) wrote some VBA code for me which (by pressing Ctrl+h) hides all menu, tool, & status bars in an Excel spreadsheet thereby enabling only the spreadsheet content to be displayed (Ctrl+r reverses the process).* The tree configuration and code itself are included below.

The problem I'm experiencing is that whenever the Excel file containing the code is running, it produces unwanted effects elsewhere (ie in other open spreadsheets). For example, it often causes changes in which bars are displayed in other open Excel files. Also, having other files open sometimes prevents the Formula Bar from being hidden in the coded file. Another problem when the coded file is running is that I cannot use Ctrl+h as a shortcut to open the Replace Box in non-coded spreadsheets.

The code was written back during the time when multiple workbooks could be opened within a single instance of Excel. At that time (ie with those earlier versions of Excel), none of the unwanted effects were present. My hope is that there is a way to modify the code so that it functions in Excel 2013 as it did originally (ie not be affected by other running Excel files nor have any effect on them).

Thanks

FigModule1:
Option Explicit

Sub hideItAll()

    With ActiveWindow
        .DisplayHorizontalScrollBar = False 'hide horizontal scroll bar
        .DisplayVerticalScrollBar = False 'hide vertical scroll bar
        .DisplayWorkbookTabs = False 'hide sheet tabs
        .DisplayHeadings = False 'hides row and column headings
    End With
    With Application
        'or you could do this for both scrollbars - currently commented
        .DisplayScrollBars = False
        
        .DisplayStatusBar = False 'hide status bar
        .DisplayFullScreen = False 'makes QAT, formula bar and status bar disappear while in full screen mode
        .DisplayFormulaBar = False 'hide formula bar
    End With
        
    Call Title_Hide 'Hides quite a bit more than just the title bar, so use at your option, and comment out the above that are not necessary, depending on what you're trying to do.
End Sub
Sub resetAllBack()

    With ActiveWindow
        .DisplayHorizontalScrollBar = True 'hide horizontal scroll bar
        .DisplayVerticalScrollBar = True 'hide vertical scroll bar
        .DisplayWorkbookTabs = True 'hide sheet tabs
        .DisplayHeadings = True 'hides row and column headings
    End With
    With Application
        'or you could do this for both scrollbars - currently commented
        .DisplayScrollBars = True
        
        .DisplayStatusBar = True 'hide status bar
        .DisplayFullScreen = False 'makes QAT, formula bar and status bar disappear while in full screen mode
        .DisplayFormulaBar = True 'hide formula bar
    End With
    
    Call Title_Show 'Shows quite a bit more than just the title bar, so use at your option, and comment out the above that are not necessary, depending on what you're trying to do.
End Sub

Open in new window

Module2:
Option Explicit
'Source:http://www.eileenslounge.com/viewtopic.php?f=27&t=1986
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
  (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
  (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
  (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Private Const GWL_STYLE = (-16)
Private Const WS_CAPTION = &HC00000
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_SYSMENU = &H80000

Private Declare Function SetWindowPos Lib "user32" _
  (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
  ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
  ByVal cy As Long, ByVal wFlags As Long) As Long

Private Enum ESetWindowPosStyles
  SWP_SHOWWINDOW = &H40
  SWP_HIDEWINDOW = &H80
  SWP_FRAMECHANGED = &H20
  SWP_NOACTIVATE = &H10
  SWP_NOCOPYBITS = &H100
  SWP_NOMOVE = &H2
  SWP_NOOWNERZORDER = &H200
  SWP_NOREDRAW = &H8
  SWP_NOREPOSITION = SWP_NOOWNERZORDER
  SWP_NOSIZE = &H1
  SWP_NOZORDER = &H4
  SWP_DRAWFRAME = SWP_FRAMECHANGED
  HWND_NOTOPMOST = -2
End Enum

Private Declare Function GetWindowRect Lib "user32" _
  (ByVal hwnd As Long, lpRect As RECT) As Long

Private Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type

Sub Title_Show()
  ShowTitleBar True
End Sub

Sub Title_Hide()
  ShowTitleBar False
End Sub

Sub ShowTitleBar(bShow As Boolean)
  Dim lStyle As Long
  Dim tRect As RECT
  Dim xlHnd As Long

  xlHnd = Application.hwnd

  '// Get the window's position:
  GetWindowRect xlHnd, tRect

  '// Show the Title bar ?
  If Not bShow Then
    lStyle = GetWindowLong(xlHnd, GWL_STYLE)
    lStyle = lStyle And Not WS_SYSMENU
    lStyle = lStyle And Not WS_MAXIMIZEBOX
    lStyle = lStyle And Not WS_MINIMIZEBOX
    lStyle = lStyle And Not WS_CAPTION
  Else
    lStyle = GetWindowLong(xlHnd, GWL_STYLE)
    lStyle = lStyle Or WS_SYSMENU
    lStyle = lStyle Or WS_MAXIMIZEBOX
    lStyle = lStyle Or WS_MINIMIZEBOX
    lStyle = lStyle Or WS_CAPTION
  End If

  SetWindowLong xlHnd, GWL_STYLE, lStyle

  Application.DisplayFullScreen = Not bShow

  '// Ensure the style is set and makes the xlwindow the
  '// same size, regardless of the title bar.
  SetWindowPos xlHnd, 0, tRect.Left, tRect.Top, tRect.Right - tRect.Left, _
    tRect.Bottom - tRect.Top, SWP_NOREPOSITION Or SWP_NOZORDER Or SWP_FRAMECHANGED
End Sub

Open in new window

* Original thread is here: View nothing but cells in Excel spreadsheet
0
Comment
Question by:Steve_Brady
  • 3
  • 2
5 Comments
 
LVL 21

Accepted Solution

by:
Ejgil Hedegaard earned 500 total points
ID: 39745230
Insert this as the first line in the 2 subs hideItAll and resetAllBack

If ActiveWorkbook.Name <> ThisWorkbook.Name Then End

Then the hide and reset code will only run when the workbook with the code is active.

Change your shortcuts to Ctrl+Shift+h and Ctrl+Shift+r, then you can use the normal Ctrl+h to display the replace box.
It is generally better to use Ctrl+Shift+Letter, than Ctrl+Letter.
That avoids conflict with the buildin shortcuts.
0
 

Author Comment

by:Steve_Brady
ID: 39748646
Thanks for the response.

After inserting the line you included in the way you described, pressing Ctrl+h while in non-coded spreadsheets did nothing at all. I tried it a couple of times in different spreadsheets but it just seems to eliminate Ctrl+h as a shortcut. By the way, Ctrl+f continued to work just fine.
0
 
LVL 21

Expert Comment

by:Ejgil Hedegaard
ID: 39750292
Ctrl+h still runs for every workbook but due to the line the macro is aborted, and do nothing (End stops the sub), when activated when another workbook is active.
But when Ctrl+h is set to activate the macro, it overrides the normal build in use of Ctrl+h, ie. display the replace box.
That is why I recommend to change macro activation to Ctrl+Shift+h.
Then Ctrl+h, display the replace box, will also work in the workbook with the hide sub.
0
 

Author Comment

by:Steve_Brady
ID: 39752954
OK, thank you for that tip.

Do you know of any solutions for solving the problems I described with the menu/tools/status bars?
0
 
LVL 21

Expert Comment

by:Ejgil Hedegaard
ID: 39756524
Some of the properties refers to Application and that affects all workbooks.
I don't have a solution to prevent that, sorry.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question