0

i'm clueless, i'm trying to build a code that input a prefix to a cell value after i change that cell, i mean i'll select a cell and input "342" for example, after i update that value i want the private sub to change that cell value to "GO-342", i've tried this, but it dosen't work.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$3" Then

If Left(Range("D3"), 2) = "GO" Then Exit Sub

Range("D3") = "GO-" & Range("D3")

End If
End Sub

the entire code:

    Private Sub Worksheet_Change(ByVal Target As Range)

'Cabeçalho
    Dim rng As Range
    Set rng = Range("D3,D5,I3,O3,O5,O7,X3,X5")
    If Intersect(Target, rng) Is Nothing Then Exit Sub
    For Each R In rng
        If R.Value = "" Then
            Exit Sub
        End If
    Next R
    Create

'Km
    Dim rng1 As Range
    Set rng1 = Range("X3,X5")
    If Intersect(Target, rng1) Is Nothing Then Exit Sub
    For Each R In rng1
        If R.Value = "" Then
            Exit Sub
        End If
    Next R
    Km

'GO
    If Target.Address = "$D$3" Then
    If Left(Range("D3"), 2) = "GO" Then Exit Sub
    Application.EnableEvents = False
        Range("D3") = "GO-" & Range("D3")
    Application.EnableEvents = True
    End If

    End Sub

"Cabeçalho" and "Km" works but "GO" dosen't

Ygor Yansz
  • 176
  • 1
  • 4
  • 12
  • Have you tried stepping through with the debugger? Where is it not doing what you expct it to do? – Degustaf Feb 19 '15 at 20:09
  • Works absolutely fine for me. Where have you placed the code? – Mat Richardson Feb 19 '15 at 20:16
  • i've placed on the worksheet, for me it just dosen't work, i leave the cell and the value dosen't change i change cell press enter and stay just with the number. it dosen't add the prefix – Ygor Yansz Feb 20 '15 at 14:27

1 Answers1

0

Here is a tiny mod to your code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$3" Then
    If Left(Range("D3"), 2) = "GO" Then Exit Sub
    Application.EnableEvents = False
        Range("D3") = "GO-" & Range("D3")
    Application.EnableEvents = True
End If
End Sub

The code must be placed in the worksheet code area.
Macros must be enabled.

Gary's Student
  • 95,722
  • 10
  • 59
  • 99