Worksheet_SelectionChange code for Data Validation

jeffreybrown

Well-known Member
Joined
Jul 28, 2004
Messages
5,149
Hi All,

I am trying to combine two Worksheet_SelectionChange events into one, but with the code below I get a compile error: Else without If. What can be done to correct this?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Target.Address <> "$B$4" Then Exit Sub
    ActiveWindow.Zoom = 120
    SendKeys "%{down}"
    Else
    ActiveWindow.Zoom = 100
    End If
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

jeffreybrown

Well-known Member
Joined
Jul 28, 2004
Messages
5,149
Hi Andrew,

That doesn't seem to help. If I remove the Exit Sub then the procedure works on all cells and not just the B4 where I have the DV.
 
Upvote 0

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
Try this
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Target.Address = "$B$4" Then 
        ActiveWindow.Zoom = 120
        SendKeys "%{down}"
    Else: ActiveWindow.Zoom = 100
    End If
End Sub

lenze
 
Upvote 0

jeffreybrown

Well-known Member
Joined
Jul 28, 2004
Messages
5,149
Yes Lenze thanks. Why does the : make a difference?
 
Upvote 0

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
The ":" just saves a line by allowing you to use the same line if you only have one line of code

lenze
 
Upvote 0

Forum statistics

Threads
1,191,214
Messages
5,985,312
Members
439,957
Latest member
venky2002

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top