Worksheet_SelectionChange code for Data Validation

jeffreybrown

Well-known Member
Joined
Jul 28, 2004
Messages
5,152
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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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
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
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,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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
Back
Top