Results 1 to 10 of 10

Thread: Excel VBA Hide ScrollBars

  1. #1

    Thread Starter
    Lively Member TheFIDDLER's Avatar
    Join Date
    May 2002
    Location
    here and there and far away
    Posts
    126

    Excel VBA Hide ScrollBars

    Can anyone explain to me why the following code works sometime and not at other times.

    If ActiveWindow.DisplayHorizontalScrollBar = False Then ActiveWindow.DisplayHorizontalScrollBar = True

    If ActiveWindow.DisplayVerticalScrollBar = False Then ActiveWindow.DisplayVerticalScrollBar = True

    I am trying to have the scroll bars activate when I activate a sheet. I use similar code to deactivate them once I leave the sheet.

    I have tried this in both the Sheet.activate module and then in a separate module in a macro that takes me from to the sheet.

    It seems that often, the very first time I run it, I get an error message saying I am unable to set the property. ALso happens when I switch from a workbook back to this one, (usually one sheet before this sheet, and then press my command button to take me to the sheet). Funny thing is that if I press the button again, (right after pressing ok to clear the error message), it works.

    I can even include an On Error Resume Next statement, and still get my error message.

    Any suggestions?
    -----
    #VBA, VB 6 Professional Edition, Office XP Developper. Excel 97, Excel 2000, Excel XP

    I miss my VIC 20.
    Never should have upgraded to my commodore 64. ...

  2. #2

    Thread Starter
    Lively Member TheFIDDLER's Avatar
    Join Date
    May 2002
    Location
    here and there and far away
    Posts
    126
    Did some trial on this and realized the cause.
    If my focus is not a worksheet, that's when I get the error.

    Usually happens if my focus is on a combo box prior to changing to the new sheet.

    But I still want to hide the scrollbars.

    Can I access this without going through the activewindow object?
    -----
    #VBA, VB 6 Professional Edition, Office XP Developper. Excel 97, Excel 2000, Excel XP

    I miss my VIC 20.
    Never should have upgraded to my commodore 64. ...

  3. #3
    Junior Member Shattered's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    26

    Re: Excel VBA Hide ScrollBars

    Originally posted by TheFIDDLER

    I am trying to have the scroll bars activate when I activate a sheet. I use similar code to deactivate them once I leave the sheet.
    Just an observation.. When excel activates and deactivates the scroll bar it does it for the actual workbook, not just that individual worksheet.

    You can remove the scrollbars whenever your workbook is loaded and remove them when its exited but not on each sheet within the book..

  4. #4

    Thread Starter
    Lively Member TheFIDDLER's Avatar
    Join Date
    May 2002
    Location
    here and there and far away
    Posts
    126
    The point is to get past the global setting and simulate worksheet level controls.

    I want to tie this to a worksheet activate and a worksheet deactivate subroutine, which in essence turns on or off the workbook global setting.

    Problem arises when my focus goes from a non sheet object, such as a combo box to the new sheet. Seems to run the activewindow.displayhorizontalscrollbar = true while still on the combo box and not on the new sheet. Hence I get an error message as the combobox (and not the new sheet) cannot support the displayhorizontalscrollbar property.

    And were it not for the combobox, this whole thing would work fine.
    -----
    #VBA, VB 6 Professional Edition, Office XP Developper. Excel 97, Excel 2000, Excel XP

    I miss my VIC 20.
    Never should have upgraded to my commodore 64. ...

  5. #5
    Junior Member Shattered's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    26
    hmm, would be fascinated to see how this could work. As it stands excel uses the active window to affect the scroll bars, not the workbook object or worksheet object, therefore seperating it out seems to be a fruitless task. One possible way around it is to use vb to launch an instance of the object and utilise the api calls to remove the scroll bars.

    Not sure why you would want to but will be interested to see if anyone does manage it..
    "much to learn you still have"

  6. #6
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    I can't duplicate the error?

    In the Sheet1 object module I have:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Worksheet_Activate()
    4.     With ActiveWindow
    5.         .DisplayHorizontalScrollBar = True
    6.         .DisplayVerticalScrollBar = True
    7.     End With
    8. End Sub
    9.  
    10. Private Sub Worksheet_Deactivate()
    11.     With ActiveWindow
    12.         .DisplayHorizontalScrollBar = False
    13.         .DisplayVerticalScrollBar = False
    14.     End With
    15. End Sub
    Works just fine. I can't see how the ActiveWindow properties would not apply to the active workbook window in the Activate event? You could always resort to something like

    VB Code:
    1. Workbooks(1).Windows(1).DisplayHorizontalScrollBar

  7. #7

    Thread Starter
    Lively Member TheFIDDLER's Avatar
    Join Date
    May 2002
    Location
    here and there and far away
    Posts
    126
    Found a partial solution.

    I can do this when using a command button to switch between sheets. All I need to do is add a "Range("A1").Select" line to my code prior to the scroll bar display command. This quick reference to part of the sheet sets the focus to the sheet and then I can access the sheet's properties.

    However, I still can't get something to work for the sheet.activate and sheet.deactivate subroutines. Also still cannot get a working model if I go direct from a combo or list box to a sheet tab.

    Will have to try adding a Range("A1").select line to the combobox change subroutine. Maybe forcing the focus back after each combo box change. Only this messes up the ability to tab between different combo and list boxes that may be visible on the screen at the same time.

    As to the why of what I am doing - I have an application done in excel, but want to hide some of the excel look. For my main menu screens, all my intro fits on one screen, and it looks sharper if the scroll bars are gone. But for my data screen, the user needs to be able to move around in the data so hence a need for scroll bars.

  8. #8

    Thread Starter
    Lively Member TheFIDDLER's Avatar
    Join Date
    May 2002
    Location
    here and there and far away
    Posts
    126
    To duplicate what I have:

    Set up a combobox on your sheet.
    Values are irrelevant, fill it with whatever.

    Set up the following code in the worksheet.activate routine for sheet(2) the sheet you will be moving to.

    If ActiveWindow.DisplayHorizontalScrollBar = False Then ActiveWindow.DisplayHorizontalScrollBar = True
    If ActiveWindow.DisplayVerticalScrollBar = False Then ActiveWindow.DisplayVerticalScrollBar = True

    Click on the combo box so that the focus changes to the combo box.

    Try - Click on a sheet tab. - error message to follow.

    Or - Code a command button with a one liner to move to another sheet. Providing the focus is not returned back to my sheet, pressing the command button while the combobox is activated gives me an error message.
    -----
    #VBA, VB 6 Professional Edition, Office XP Developper. Excel 97, Excel 2000, Excel XP

    I miss my VIC 20.
    Never should have upgraded to my commodore 64. ...

  9. #9
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Nope. No problems at all. Works like a charm. What version of Excel are you using?

  10. #10

    Thread Starter
    Lively Member TheFIDDLER's Avatar
    Join Date
    May 2002
    Location
    here and there and far away
    Posts
    126
    Excel 97 SR-1


    Also - not sure if relevant -
    but the starting sheet has scrollbars disabled.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width