仅当列不存在VBA时,才将值附加到列的最新单元格

时间:2014-08-21 18:51:36

标签: excel vba

在Excel中,我需要能够将值附加到列的最新单元格,只有它不存在。

让我们说我们需要添加一个"测试"字符串到一列单词..

COL_WORDS
chair 
car
Bus
Help

1 个答案:

答案 0 :(得分:0)

您的意思是,使用VBA,如果该值没有出现在列中(减去我假设的标题),您希望将值附加到列的末尾?如果是这样,这可能会有所帮助:

Sub AddIfNotDup(str As String)

    Dim c As Range
    Dim dupFound As Boolean
    Dim lastRow As Long

    dupFound = False
    lastRow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row

    For Each c In Sheet1.Range("$A$2:$A$" & lastRow)
        Debug.Print c.Value
        If c.Value = str Then
            dupFound = True
            Exit For
        End If
    Next c

    If dupFound = False Then
        Cells(lastRow + 1, "A").Value = str
    End If
End Sub

Sub test()
 AddIfNotDup ("mob")
End Sub