在excel中,当我在“输入栏”中输入“A”时,我想,Harry,Josh,Rob和Peter将自动填充,如图所示。
同样,当我在输入栏中输入“B”时,我希望Kim,Nancy,Paul,George自动填写。任何人都可以发布VBA代码来执行此操作,还是可以使用公式本身?
答案 0 :(得分:4)
自动代码解决方案:
当Column A
中的一个或多个单元格(即您的“目标字幕”)发生变化时,此代码会进行所需的更改
要使用B列,请尝试使用
Set rng1 = Intersect(Target, Columns("B"))
等
<强>码强>
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Intersect(Target, Columns("A"))
If rng1 Is Nothing Then Exit Sub
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
For Each rng2 In rng1
Select Case UCase$(rng2.Value)
Case "A"
rng2.Offset(0, 1).Resize(1, 2) = Array("Harry", "Josh")
rng2.Offset(1, 1).Resize(1, 2) = Array("Rob", "Peter")
Case "B"
rng2.Offset(0, 1).Resize(1, 2) = Array("Kim", "Nancy")
rng2.Offset(1, 1).Resize(1, 2) = Array("Paul", "George")
End Select
Next
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub