WorksheetChange事件连接行和名字的第一个字母+姓氏的第一个字母

时间:2017-03-27 15:00:19

标签: excel vba

我认为代码应该是这样的,但是我在这一行上遇到错误,我试图处理名字和姓氏。基本上,我想在A列中创建一个代码,这是该人的名字的第一个字母和该姓氏的第一个字母,与行号连接在一起。该行将是活动行(始终为A列),并且名字和姓氏将存储在B列中。

Private Sub Worksheet_Change(ByVal Target As Range)

  If Target.Column <> 1 Then Exit Sub

  On Error GoTo ErrHandler
  Application.EnableEvents = False
    If Target.Column = 1 Then
        Target.Offset(0, 0).FormulaR1C1 = "=ROW()"
        TV1 = Target.Offset(0, 0).FormulaR1C1
        Target.Offset(0, 0).FormulaR1C1 = "=UPPER(LEFT(R[" & "=ROW()" & "]C[1],1)&MID(R[" & "=ROW()" & "]C[1],FIND("" "",R[" & "=ROW()" & "]C[1],1)+1,1))"
        TV2 = Target.Offset(0, 0).FormulaR1C1
        Target.Offset(0, 0).Value = TV2 & "-" & TV1
    End If

End Sub

3 个答案:

答案 0 :(得分:2)

我会这样做。为什么只在VBA中编写公式?

我也对你的原始代码做了一些注释:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

  If Target.Column <> 1 Then Exit Sub

  Application.EnableEvents = False

' No error handler in your code
'On Error GoTo ErrHandler

' don't need to check if column 1 since we already did that and exited the sub if it was not
'    If Target.Column = 1 Then

        'Target.Offset(0,0) = Target
        'Target.Offset(0, 0).FormulaR1C1 = "=ROW()"
        'TV1 = Target.Offset(0, 0).FormulaR1C1
        'Target.Offset(0, 0).FormulaR1C1 = "=UPPER(LEFT(R[" & "=ROW()" & "]C[1],1)&MID(R[" & "=ROW()" & "]C[1],FIND("" "",R[" & "=ROW()" & "]C[1],1)+1,1))"
        'TV2 = Target.Offset(0, 0).FormulaR1C1
        'Target.Offset(0, 0).Value = TV2 & "-" & TV1

        'Just do the creation in VB
        With Target
            .Value = .Row & Left(.Offset(0, 1), 1) & Left(Split(.Offset(0, 1))(1), 1)
        End With

        'If you have more than two space-separated words in the name, then something like
        Dim V As Variant
        With Target
            V = Split(.Offset(0, 1))
            .Value = .Row & Left(V(0), 1) & Left(V(UBound(V)), 1)
        End With

'Don't forget to reenable events
Application.EnableEvents = True


End Sub

此外,由于名称在B列中,您为什么要测试A列中的更改?可能有原因,但如果没有,则检查B列中的更改可能会更顺畅。

答案 1 :(得分:2)

我不想避免处理多个单元格作为目标。处理多个细胞并不难。

禁用事件并执行处理后,您不会再次打开它们。您的代码只会运行一次而无需手动重新启动事件。

如果您将名字和姓氏列入B栏,那么处理是否应该受B列而不是A列的影响?

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Columns("B")) Is Nothing Then
        On Error GoTo ErrHandler
        Application.EnableEvents = False
        Dim trgt As Range
        For Each trgt In Intersect(Target, Target.Parent.UsedRange, Columns("B"))
            trgt = StrConv(Trim(trgt.Value2), vbProperCase)
            If CBool(InStr(2, trgt.Value2, Chr(32))) Then
                trgt.Offset(0, -1) = _
                    UCase(Left(trgt.Value2, 1)) & _
                    UCase(Mid(trgt.Value2, InStr(1, trgt.Value2, Chr(32)) + 1, 1)) & _
                    Format(trgt.Row, "000")
            End If
        Next trgt
    End If

ErrHandler:
  Application.EnableEvents = True
End Sub

我添加了一些修剪和正确大小写转换,以自动更正键入B列的值。

在下图中,我复制了G5:G8中的名称并将其粘贴到B2:B5中。

enter image description here

答案 2 :(得分:1)

我明白了!!

If Target.Column = 1 Then
    Target.Offset(0, 0).FormulaR1C1 = "=ROW()"
    TV1 = Target.Value
    Target.Offset(0, 0).FormulaR1C1 = "=UPPER(LEFT(RC[1],1)&MID(RC[1],FIND("" "",RC[1],1)+1,1))"
    TV2 = Target.Value
    Target.Value = TV2 & "-" & TV1
End If