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!
 

Some videos you may like

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

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!
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,371
Messages
5,510,903
Members
408,817
Latest member
jarlej

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top