Consulting

Results 1 to 9 of 9

Thread: Gradual Zoom?

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location

    Gradual Zoom?

    Hi All,
    Is there a method to gradually zoom out?

    Sub test()

    Application.WindowState = xlMaximized

    With Sheets("Sheets1")
    For i = 100 to Application.WindowState
    ActiveWindow.Zoom = i
    Next i

    End With
    End Sub


    ....Also, is there a way to zoom back to the original size?

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    Quote Originally Posted by Sully1440 View Post
    gradually
    …over time or with user interaction?

    Also, are you confusing the size of the window with the zoom factor (magnification)?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    Hi,
    Over time, I'd like a gradual zoom out after the user selects the a macro.

    I'd like it to go from current zoom state to view max zoom to include a entire view but in a gradual increment.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Do you want to have it just shrink back to original size (you have a look in the example) or be something caused by user action?


    Maybe zoomOutGrow() below might be what you're looking for


    Option Explicit
    
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Const zoomIncrement As Long = 10
    Public zoomOriginal As Long
     
    Public Sub zoomOut()
        'remember
        If zoomOriginal = 0 Then zoomOriginal = ActiveWindow.Zoom
        ActiveWindow.Zoom = ActiveWindow.Zoom - zoomIncrement
    End Sub
    
    
    Public Sub zoomIn()
        'remember
        If zoomOriginal = 0 Then zoomOriginal = ActiveWindow.Zoom
        ActiveWindow.Zoom = ActiveWindow.Zoom + zoomIncrement
    End Sub
    
    Public Sub zoomReset()
        'remember
        If zoomOriginal = 0 Then
            ActiveWindow.Zoom = 100
        Else
            ActiveWindow.Zoom = zoomOriginal
            zoomOriginal = 0
        End If
    End Sub
    
    
    Public Sub zoomOutGrow()
        
        Do While ActiveWindow.Zoom > 100
            ActiveWindow.Zoom = ActiveWindow.Zoom - 1
            Sleep 100           '   1/10 sec
            DoEvents
        Loop
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by Sully1440 View Post
    Hi,
    Over time, I'd like a gradual zoom out after the user selects the a macro.

    I'd like it to go from current zoom state to view max zoom to include a entire view but in a gradual increment.
    So if current zoom state = 120%, how do you define max zoom?

    What is 'entire view'? Columns A to last one with data?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    Quote Originally Posted by p45cal View Post
    …over time or with user interaction?

    Also, are you confusing the size of the window with the zoom factor (magnification)?

  7. #7
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    Hi Paul,
    This works great. Outgrow is what I'm looking for. One last question, is there a way to zoom out to only show what's on the sheet using the code below?

    Public Sub zoomOutGrow2()
    Do While ActiveWindow.Zoom > 20
    ActiveWindow.Zoom = ActiveWindow.Zoom - 1
    'Sleep 100 ' 1/10 sec
    DoEvents
    Loop
    End Sub

    Public Sub zoomInGrow2()
    Do While ActiveWindow.Zoom < 70
    ActiveWindow.Zoom = ActiveWindow.Zoom + 1
    'Sleep 100 ' 1/10 sec
    DoEvents
    Loop
    End Sub

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    You can try this

    Note that if you have data in A:K (the ws .UsedRange) but that only fills 1/2 the screen, the zoom will fit A:K across the screen -- is that what you want?



    Option Explicit
    
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Public Sub zoomOutGrow()
        Dim zoomSelection As Long, zoomOriginal As Long
        Dim oSelection As Object
        
        'remember start zoom
        zoomOriginal = ActiveWindow.Zoom
        
        'remember what was selected
        Set oSelection = Selection
        
        'zoom to data
        Application.ScreenUpdating = False
        ActiveSheet.UsedRange.EntireColumn.Select
        ActiveWindow.Zoom = True
        zoomSelection = ActiveWindow.Zoom
        ActiveWindow.Zoom = zoomOriginal
        Application.ScreenUpdating = True
        
        
        If zoomSelection > ActiveWindow.Zoom Then
            Do While ActiveWindow.Zoom < zoomSelection
                ActiveWindow.Zoom = ActiveWindow.Zoom + 1
                Sleep 100           '   1/10 sec
                DoEvents
            Loop
    
        ElseIf zoomSelection < ActiveWindow.Zoom Then
            Do While ActiveWindow.Zoom > zoomSelection
                ActiveWindow.Zoom = ActiveWindow.Zoom - 1
                Sleep 100           '   1/10 sec
                DoEvents
            Loop
        End If
        
        oSelection.Select
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    Yes, this works. Thanks Paul. Much appreciated.

Posting Permissions

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