Excel VBA zooming range

If Target.Address = "$D$2:$F$861" Then

    ActiveWindow.Zoom = 100
    [A5000] = "zoomed"
ElseIf [A5000] = "zoomed" Then
     'Otherwise set the zoom to original
    ActiveWindow.Zoom = 70
    [A5000].ClearContents 
End If

In the above code If Target.Address = "$D$2:$F$861" Then does not work.

I want to zoom in while user choses D2:F861.

When i type single cell adress like $A$2 it works.

Please help me that when user choose this area zoom changes to 100% otherwise it stays 70% or the other option it stays what user sets


It is not actually range comparison. It added new value even when you choose from dropdown list with comma to desired cell.

However, if we dont get bigger font from dropdown list. I want to add some code to zoom while choose the cells which have dropdown lists.


It is full code below:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strVal As String
Dim i As Long
Dim lCount As Long
Dim Ar As Variant
On Error Resume Next
Dim lType As Long
If Target.Count > 1 Then GoTo exitHandler



 If Target.Address = "$D$2:$F$861" Then

    ActiveWindow.Zoom = 100
    [A5000] = "zoomed"
ElseIf [A5000] = "zoomed" Then
     'Otherwise set the zoom to original
    ActiveWindow.Zoom = 70
    [A5000].ClearContents
End If

lType = Target.Validation.Type
If lType = 3 Then
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal





    If oldVal = "" Then
        'do nothing
    Else
        If newVal = "" Then
            'do nothing
        Else
            On Error Resume Next
            Ar = Split(oldVal, ", ")
            strVal = ""
            For i = LBound(Ar) To UBound(Ar)
                Debug.Print strVal
                Debug.Print CStr(Ar(i))
                If newVal = CStr(Ar(i)) Then
                    'do not include this item
                    strVal = strVal
                    lCount = 1
                Else
                    strVal = strVal & CStr(Ar(i)) & ", "
                End If
            Next i
            If lCount > 0 Then
                Target.Value = Left(strVal, Len(strVal) - 2)
            Else
                Target.Value = strVal & newVal
            End If
        End If
    End If

End If

exitHandler:
  Application.EnableEvents = True
End Sub


ANSWERS:


To know if Target is inside the desired Range, you can test it with this :
If Not Application.Intersect(Target, Me.Range("$D$2:$F$861")) Is Nothing Then

 Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strVal As String
Dim i As Long
Dim lCount As Long
Dim Ar As Variant
On Error Resume Next
Dim lType As Long
If Target.Count > 1 Then GoTo exitHandler


MsgBox "In event"

If Not Application.Intersect(Target, Me.Range("$D$2:$F$861")) Is Nothing Then
    MsgBox "In range"
    ActiveWindow.Zoom = 100
    Me.[A5000] = "zoomed"
Else
    If Me.[A5000] = "zoomed" Then
         'Otherwise set the zoom to original
        ActiveWindow.Zoom = 70
        Me.[A5000].ClearContents
    Else
    End If
End If


MsgBox "Check validation"
lType = Target.Validation.Type
If lType = 3 Then
    Application.EnableEvents = False
    newVal = Target.Value
    Application.Undo
    oldVal = Target.Value
    Target.Value = newVal
    If oldVal = vbNullString Then
        'do nothing
    Else
        If newVal = vbNullString Then
            'do nothing
        Else
            On Error Resume Next
            Ar = Split(oldVal, ", ")
            strVal = vbNullString
            For i = LBound(Ar) To UBound(Ar)
                Debug.Print strVal
                Debug.Print CStr(Ar(i))
                If newVal = CStr(Ar(i)) Then
                    'do not include this item
                    strVal = strVal
                    lCount = 1
                Else
                    strVal = strVal & CStr(Ar(i)) & ", "
                End If
            Next i
            If lCount > 0 Then
                Target.Value = Left(strVal, Len(strVal) - 2)
            Else
                Target.Value = strVal & newVal
            End If
        End If
    End If
End If

exitHandler:
  Application.EnableEvents = True
End Sub


 MORE:


 ? Scale PDF to fit iFrame
 ? In d3.js v4 zoom functionality I am having an issue where when i zoom out and then pan the chart goes out of the viewport instantly
 ? Error in background zoom?
 ? Sample code for using camera digital zoom in Android-app?
 ? WPF 3D: Zoom model with mouse pointer coordinates as center
 ? WPF image zooming
 ? Trying to make a zoom effect
 ? How to make the subview in a UIScrollView to refresh when we zoom it?
 ? iPhone Zoom Transition between 2 UIViews
 ? UIWebview won't zoom even after setting scalesPageToFit to YES