Consulting

Results 1 to 6 of 6

Thread: Individual zoomfactor for each sheet

  1. #1
    VBAX Regular
    Joined
    Jan 2017
    Location
    Warsaw
    Posts
    70
    Location

    Individual zoomfactor for each sheet

    Hey guys!


    I am currently working on developing a tool for describing and presenting processes in monetary terms. This tool consists of 5 tables (sheets).Each of these 5 sheets has its own width, which means that I or the user must first correct the sheets before applying them.

    Is it possible to use VBA to set the zoom range for each sheet, which in Table1, for example, concentrates the zoom range on column 2 and 10, and in table 2 on column 2 and 12?
    An individuell zoomfactor for each sheet with regard to the respektive user (formats).



    I hope I could represent the problem with my words (:


    Best regards!

    I hope I could represent the problem with my words.


    Edit:
    I think this is maybe a possible approach:
    
    
    Worksheets("Tabelle1").Range("B3:K3").Select
    ActiveWindow.zoom = True
    But I don't know how to apply this on/ for each sheet immediately after starting/ opening the tool
    Last edited by joshua1990; 09-08-2017 at 06:45 AM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    In the ThisWorkbook code module add these event handlers, changing / adding as required

    The column widths are hard coded for each sheet, but there's probably a way to make the macros more elegant depending on data structure

    Option Explicit
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
        
        Select Case Sh.Name
            Case "Sheet1"
                Columns("B:I").Select
                ActiveWindow.Zoom = True
                Range("B1").Select
            Case "Sheet2"
                Columns("B:K").Select
                ActiveWindow.Zoom = True
                Range("B1").Select
        End Select
        
    End Sub
    
    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
            ActiveWindow.Zoom = 100
    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

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Option Explicit
    
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
        Dim r As Range
        On Error Resume Next
        Set r = Range(Sh.ListObjects.Item(1))
        If Not r Is Nothing Then
        r.Select
        With ActiveWindow
            .Zoom = True
            .ScrollRow = r.Cells(0, 1).Row
            .ScrollColumn = r.Cells(0, 1).Column
        End With
        r.Cells(0, 1).Select
        End If
    End Sub
    Last edited by mdmackillop; 09-08-2017 at 07:42 AM. Reason: Error handling added for non-table sheets
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Regular
    Joined
    Jan 2017
    Location
    Warsaw
    Posts
    70
    Location
    Quote Originally Posted by Paul_Hossler View Post
    In the ThisWorkbook code module add these event handlers, changing / adding as required

    The column widths are hard coded for each sheet, but there's probably a way to make the macros more elegant depending on data structure

    Option Explicit
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
        
        Select Case Sh.Name
            Case "Sheet1"
                Columns("B:I").Select
                ActiveWindow.Zoom = True
                Range("B1").Select
            Case "Sheet2"
                Columns("B:K").Select
                ActiveWindow.Zoom = True
                Range("B1").Select
        End Select
        
    End Sub
    
    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
            ActiveWindow.Zoom = 100
    End Sub
    Thanks a lot!
    This works great!

    But now it's updating the zoom factor every time I switch the sheets. Is it possible, to start this procedure only as an initial action? Just at the start/ opening of the file?

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    1. Added Mac's code

    2. Put in ThisWorkbook, add your sheets and ranges, and see if it works for you

    Option Explicit
    Private Sub Workbook_Open()
        Dim ws As Worksheet
            
        Application.ScreenUpdating = False
            
        For Each ws In ThisWorkbook.Worksheets
            With ws
                Select Case ws.Name
                    Case "Sheet1": Call ZoomFit(.Range("B:I"))
                    Case "Sheet2": Call ZoomFit(.Range("B:K"))
                End Select
            End With
        Next
        Application.ScreenUpdating = True
    End Sub
    
    
    Private Sub ZoomFit(r As Range)
        r.Parent.Select
        r.EntireColumn.Select
        
        With ActiveWindow
            .Zoom = True
            .ScrollRow = r.Cells(1, 1).Row
            .ScrollColumn = r.Cells(1, 1).Column
        End With
        
        r.Cells(1, 1).Select
    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

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Just my personal opinion, but this is a situation where it is more appropriate to encode each sheet individually.

    Ie:
    Sheet_Activate()
    Zoom As Desired
    End


    Sheet_Deactivate()
    UnZoom if needed
    End
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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