用于搜索记录的公式如果找到则添加1,否则它将创建新记录

时间:2017-03-08 21:30:07

标签: excel vba excel-vba excel-formula

我是Excel的新手,对此感到抱歉,但是你能帮我提供一些我可以用来解决问题的资源吗?

我想要一个简单的公式,用户可以输入用户名

forumla在所有A:A单元格中搜索用户名,如果找到它,它会在右边的单元格中添加1,否则会在底部打开的单元格中生成用户名。

example here

1 个答案:

答案 0 :(得分:0)

这是一个与公式相同的宏:

将其放入您的工作表代码

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$F$4" Then 'assuming the name you are searching is in F4
    Dim i As Integer
    Dim check As Integer

    Dim LastRow As Long
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    For i = 2 To LastRow
        If Cells(i, 1).Value = Target.Value Then
            Cells(i, 1).Offset(0, 1).Value = 1
            check = 1
        End If
    Next i

    If check > 0 Then
        Exit Sub
    Else
        LastRow = LastRow + 1
        Cells(LastRow, 1).Value = Target.Value
    End If
End If
End Sub