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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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
 
Upvote 0
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!
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,046
Members
449,063
Latest member
ak94

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