0

我目前有这个代码:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim lastrow As Long
    Dim rngList As Range

    lastrow = Cells(Rows.Count, "A").End(xlUp).Row

    Set rngList = Range("AB3").CurrentRegion

    If Target.Cells.Count > 1 Then Exit Sub

    On Error Resume Next

    If Not Intersect(Target, Range("B18:B19")) Is Nothing Then  ' user is in column-A
        Target.Value = Application.WorksheetFunction.VLookup(Target.Value, rngList, 2, False)
    End If

    Set rngList = Nothing
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

Dim lastrow As Long
Dim rngList As Range

lastrow = Cells(Rows.Count, "A").End(xlUp).Row

Set rngList = Range("AC3").CurrentRegion

If Target.Cells.Count > 1 Then Exit Sub

On Error Resume Next

If Not Intersect(Target, Range("B10:B11")) Is Nothing Then  ' user is in column-A
    Target.Value = Application.WorksheetFunction.VLookup(Target.Value, rngList, 2, False)
End If

Set rngList = Nothing

结束子

我想将它们结合起来,以便我可以同时使用它们,但我不知道如何在没有冲突的情况下这样做,任何帮助将不胜感激,谢谢。

4

1 回答 1

0

如果我对您的理解正确,这应该可以解决您尝试做的事情:

Private Sub Worksheet_Change(ByVal Target As Range)

    WorksheetChanged Target, Range("AC3").CurrentRegion, Range("B10:B11")
    WorksheetChanged Target, Range("AB3").CurrentRegion, Range("B18:B19")

结束子

Private Sub WorksheetChanged(ByVal Target As Range, ByVal rngList As Range, ByVal intersectRng As Range)

    Dim lastrow 只要

    lastrow = Cells(Rows.Count, "A").End(xlUp).Row

    如果 Target.Cells.Count > 1 然后退出 Sub

    出错时继续下一步

    If Not Intersect(Target, intersectRng) Is Nothing Then ' 用户在列-A
        Target.Value = Application.WorksheetFunction.VLookup(Target.Value, rngList, 2, False)
    万一

    设置 rngList = 无
结束子

在这里,您有一个私有函数,它将范围作为参数。您可以根据需要随时调用此函数(尽管如果您在 Worksheed_Change Sub 中调用过多,Excel 可能会变得有点慢)。

于 2012-06-28T11:06:12.940 回答