VBA代码或宏复制单元格值

时间:2017-10-28 07:03:00

标签: excel vba excel-vba

我没有使用macro / vba的经验。

picture

正如您所看到的,所有A栏都有PINK TEAM标签但不同类别(网球,游泳,排球)。在我的电子表格中,有很多PINK TEAM标签一直向下但不同的类别(网球,游泳等)。我希望能够添加名称,例如将Remy添加到Pink Team Tennis,所有使用PINK TEAM / Tennis的单元格都应该能够在C列上自动添加REMY。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

试试这个:

Sub AddName()
    Dim lr As Long, i As Long, count As Long
    Dim Team As String, Sport As String, NewPlayer As String

    Team = "Pink Team"
    Sport = InputBox("What Sport do you wish to add a name to?", "Sport")
    NewPlayer = InputBox("What is the name of the player you wish to add?", "New Player")
        With ActiveSheet
            lr = .Range("A" & Rows.count).End(xlUp).Row

            For i = 1 To lr
                If .Range("A" & i).Value = Team And .Range("B" & i).Value = Sport Then
                    .Range("C" & i).Value = .Range("C" & i).Value & ", " & NewPlayer
                    count = count + 1
                End If
            Next i
        End With
    If count = 0 Then MsgBox "The " & Team & " doesnt appear to play " & Sport & " yet."
End Sub

在键盘上添加代码ALT + F11。然后双击要添加代码的工作表名称(左上角)到&将代码粘贴在那里。