Excel/VBA alteration

Advertisement


Question
Hi Tom

This VBA will magnify the size of the page to 120% when the data validation box is selected

Can the code be altered so that once the selection has been made from the dropdown box the sheet size will revert to its previous size of 100%?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 Dim lZoom As Long
 Dim lZoomDV As Long
 Dim lDVType As Long
 lZoom = 100
 lZoomDV = 120
 lDVType = 0

 Application.EnableEvents = False
 On Error Resume Next
 lDVType = Target.Validation.Type
 
   On Error GoTo errHandler
   If lDVType <> 3 Then
     With ActiveWindow
       If .Zoom <> lZoom Then
         .Zoom = lZoom
       End If
     End With
   Else
     With ActiveWindow
       If .Zoom <> lZoomDV Then
         .Zoom = lZoomDV
       End If
     End With
   End If

exitHandler:
 Application.EnableEvents = True
 Exit Sub
errHandler:
 GoTo exitHandler
End Sub

Thanks in advance

Chris Mitchell

Answer
Chris Mitchell,

The code is set up to work when the selection changes.  So it will not react to a change editing the cell.  As long as the cell selection changes after a selection is made, then it should revert to the old zoom level if that new selection isn't using a dropdown list from data validation.  


If you wanted to add code to the change event you could do this

Private Sub Worksheet_Change(ByVal Target As Range)
 Dim lZoom As Long
 Dim lZoomDV As Long
 Dim lDVType As Long
 lZoom = 100
 lZoomDV = 120
 lDVType = 0

 Application.EnableEvents = False
 On Error Resume Next
   lDVType = Target.Validation.Type
if lDVType = 3 then
With ActiveWindow
  If .Zoom <> lZoom Then
     .Zoom = lZoom
  End If
End With
End if
Application.EnableEvents = True
End Sub

That worked for me, but I  only had those two events in the sheet module and did minimal testing.  

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.