Solved

Odd Behavior with Used Range Zoom Macro

Posted on 2014-10-10
15
235 Views
Last Modified: 2014-10-15
Experts,

Please see the attached file and the below code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    ActiveSheet.UsedRange.Resize(ActiveSheet.UsedRange.Columns.Count).Select
    ActiveWindow.Zoom = True: ActiveWindow.VisibleRange(1, 1).Select
End Sub

Open in new window

Why does the zoom range set differently between sheet1 and sheet2 when some of the rows are different heights but it is the same number of used columns?

Is there a better way to write this? I would like for it to only set the active zoom based columns with used cells.....
EE-UsedZoomRange-Test.xlsm
0
Comment
Question by:-Polak
  • 7
  • 6
15 Comments
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40374279
use this instead of that

Sub UsedRangeZoom()
    'set zoom for active sheet to columns with data
Application.EnableEvents = True
    Application.WindowState = xlMaximized
    ActiveSheet.UsedRange.Select
    ActiveWindow.Zoom = True
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:-Polak
ID: 40374301
JimJam your code does the same thing 86% zoom on sheet1 76% zoom on sheet2.....
0
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40374303
does both of the sheets has similar amount data in their cells?
0
 
LVL 1

Author Comment

by:-Polak
ID: 40374305
I attached the example. Yes sheet1 and sheet2 are identical, except in sheet2 the row heights have been modified.
0
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40374322
check the file that i hereby attached.  let me know how the zoom works.
sssss.xlsm
0
 
LVL 1

Author Comment

by:-Polak
ID: 40374877
I got a runtime error 91 as soon as tired to open that file.
I've reattached your workbook with the bits of code not pertinent to this question disabled. Lets just focus on the Zoom issue not any of the environment things you are helping me on in another question....

For me your code doesn't set the zoom at all for used cells in the attached. Moreover your code doesn't even allow me to switch between sheet1 and sheet2.
Copy-of-sssss.xlsm
0
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40375201
OK,  please see attached file. it is 100% working. I checked it myself and it works.
F--Auto-Zoom-VBA.xlsm
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:-Polak
ID: 40377455
Question, what are you zoom %'s on sheet1 vs. sheet2? (mine are 86% vs 72%) pretty much the same result as my original code.....
0
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40377464
it depends on the monitor of your screen.  min eis 90 and next one 103
0
 
LVL 1

Author Comment

by:-Polak
ID: 40377481
Given that the "Used Range" is the same between both sheets why the difference just because sheet2 has some modified row heights?
0
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40377699
yes exaclty
Row Heights affects zoom range and that is why, there is difference.
0
 
LVL 1

Author Comment

by:-Polak
ID: 40377740
Going back to my original need, I only want the zoom range to set based upon used COLUMNS, I do not want row heights to effect anything; my original code shouldn't behave that way yet it does....
0
 
LVL 21

Accepted Solution

by:
Ejgil Hedegaard earned 500 total points
ID: 40383039
You select the entire used range, and make the window zoom on that selection.
Then the rows also need to fit the window,
To only zoom on the columns, select the first row only.

So the macro will be

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    ActiveSheet.UsedRange.Rows(1).Select
    ActiveWindow.Zoom = True
    ActiveWindow.VisibleRange(1, 1).Select
End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:-Polak
ID: 40383105
Hummm, okay I see what I did wrong, thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now