0

我有一个为客户服务定义的 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"
4

1 回答 1

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

从这个开始...

在此处输入图像描述

然后在单元格 A4 中输入 LON9501 得到这个......

在此处输入图像描述

于 2016-04-09T22:23:33.073 回答