Problem with event handler macro.

brans1982

Active Member
Joined
Mar 25, 2009
Messages
263
Hi,

Could someone take a look at this.

I created this Event Handler to add "-" in the middle of a 6 digit string...

e.g aaabbb will format as AAA-BBB.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Select Case Target.Column
Case 9
Routing = Target.Value
Select Case Len(Routing)
Case 6
NewRouting = UCase(Left(Routing, 3) & "-" & Right(Routing, 3))
End Select

Target.Value = NewRouting

End Select

End Sub


Problem is, after typing data into the cell and hitting enter, I then have reselect the cell fot the macro to run.

Any ideas?

Thanks!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
You are using the wrong event. Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Target.Column
    Case 9
        Application.EnableEvents = False
        Routing = Target.Value
            Select Case Len(Routing)
                Case 6
                    NewRouting = UCase(Left(Routing, 3) & "-" & Right(Routing, 3))
            End Select

        Target.Value = NewRouting
        Application.EnableEvents = True
    End Select

End Sub
 

brans1982

Active Member
Joined
Mar 25, 2009
Messages
263
Thanks Peter,

That worked perfect.

Is it possible to have two event handlers on one worksheet?

If so, how would I put this in...

Select Case Target.Column
Case 4
Application.EnableEvents = False
Order = Target.Value
Select Case Len(Order)
Case 11
NewOrder = Left(Order, 3) & "-" & Right(Order, 8))
End Select

Target.Value = NewOrder
Application.EnableEvents = True
End Select


Thanks!
 

Watch MrExcel Video

Forum statistics

Threads
1,133,383
Messages
5,658,494
Members
418,448
Latest member
nikwak1

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