运行宏以在值更新后更改单元格

时间:2015-02-19 20:05:46

标签: excel vba worksheet-function private-methods

我很无能,我正在尝试构建一个代码,在我更改该单元格之后为单元格值输入前缀,我的意思是我将选择一个单元格并输入“342”,例如,在我更新之后值我希望私有子将该单元格值更改为“GO-342”,我已经尝试过了,但它不起作用。

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

整个代码:

    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”和“Km”有效,但“GO”不是

1 个答案:

答案 0 :(得分:0)

以下是代码中的一个小mod:

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

代码必须放在工作表代码区域。
必须启用。