Link to home
Start Free TrialLog in
Avatar of Steve_Brady
Steve_BradyFlag for United States of America

asked on

Prevent VBA written for one Excel workbook from affecting other workbooks

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

User generated imageModule1:
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
ASKER CERTIFIED SOLUTION
Avatar of Ejgil Hedegaard
Ejgil Hedegaard
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steve_Brady

ASKER

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.
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.
OK, thank you for that tip.

Do you know of any solutions for solving the problems I described with the menu/tools/status bars?
Some of the properties refers to Application and that affects all workbooks.
I don't have a solution to prevent that, sorry.