一旦达到第一列中的复制值,如何自动填充行的其余部分

时间:2016-04-09 20:46:05

标签: excel excel-vba excel-formula excel-2016 vba

我有一个为客户服务定义的excel表。如果为将来的服务重复Customer_ID的值,我想使用VB进行行自动填充以获得列A(Customer_ID)的相关值。那么,我怎么能在同一张纸上做呢?行内容的示例是:

"Customer_ID"   "Identity_No."   "City"   "Customer's_Name"   "Phone_No."   "Email"

"LON9501"       "22024245423"   "London"   "John_Nash"        "9234784546"  "foobar@gmail.com"

1 个答案:

答案 0 :(得分:1)

将以下内容附加到工作表的更改事件...

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ARng As Range
    Dim myRng As Range
    Dim iCnt As Long, iLoop As Long
    Dim found As Boolean

    Set ARng = ActiveSheet.UsedRange
    Set ARng = ARng.Cells(ARng.Rows.Count, 1)

    If Not Intersect(Target, ARng) Is Nothing Then 'Check the last cell in column A changed
        Set myRng = ActiveSheet.UsedRange
        iCnt = myRng.Rows.Count
        found = False
        Do While Not found And iCnt > 0 'Search for another instance from the bottom up
            iCnt = iCnt - 1
            found = Target.Value = myRng.Cells(iCnt, 1).Value
            If found Then 'found another instance so populate the row
                For iLoop = 2 To myRng.Columns.Count
                    myRng.Cells(myRng.Rows.Count, iLoop) = myRng.Cells(iCnt, iLoop)
                Next iLoop
            End If
        Loop
    End If
End Sub

从这开始...

enter image description here

然后在单元格A4中输入LON9501就可以了......

enter image description here