我目前有这段代码:
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
End Sub
我想将它们结合起来,以便我可以同时使用它们,但我不知道如何在没有冲突的情况下这样做,任何帮助都将不胜感激,谢谢。
答案 0 :(得分: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") End Sub Private Sub WorksheetChanged( ByVal Target As Range, ByVal rngList As Range, ByVal intersectRng As Range ) Dim lastrow As Long lastrow = Cells(Rows.Count, "A").End(xlUp).Row If Target.Cells.Count > 1 Then Exit Sub On Error Resume Next If Not Intersect(Target, intersectRng) 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
这里有一个私有函数,它将Ranges作为参数。您可以根据需要随时调用此函数(如果在Worksheed_Change Sub中调用它太多,Excel可能会变得有点慢)。