【发布时间】:2018-01-03 06:18:02
【问题描述】:
我有一张包含项目代码 (Col. B)、描述 (Col. C)、单位 (Col. D)、Location1_Rate (Col. E)、Location2_Rate (Col. F)、Location3_Rate (Col. G) 的 MASTER 表)、Location4_Rate (Col.H)、Location5_Rate (Col.I)、Location6_Rate (Col.J)、Location7_Rate (Col.K)
我使用如下配置的 OUTPUT 表
位置选择(Location_1 到 Location_7)作为下拉菜单:A16
项目代码:D 栏(需手动输入)
说明:F上校
数量:G上校
单位:H校
评分:第一上校
金额:J 校
每当我在 OUTPUT 表中输入项目代码 (Col. D) 时,项目的描述以及相应位置的费率应分别自动出现在 Col.F 和 Col. I 的相应单元格中。第一个项目代码以单元格 D20 开头。
因为,我的目标是保留 OUTPUT 表中的格式,所以我使用 VBA 代码作为描述字段。我在返回单个位置 (Location1_Rate) 的速率的 VBA 代码中成功。但是,我无法修改代码以获取不同位置的费率(基于单元格 A16 中的位置)。请仔细阅读代码并提出建议。
注意:我有多个 MASTER 工作表用于查找项目代码,因此我提到的 VBA 考虑了跨多个工作表的查找。但是,只有 MASTER Sheet1 需要进行上述二维查找。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Fnd As Range
Dim NotFnd As Boolean
Dim Ws As Worksheet
NotFnd = False
If Target.CountLarge > 1 Then Exit Sub
If Not Target.Column = 4 Then Exit Sub
Application.EnableEvents = False
For Each Ws In Worksheets
If Not Ws.Name = "Output" Then
Set Fnd = Ws.Columns(2).Find(Target.Value, , , xlWhole, , , False, , False)
If Not Fnd Is Nothing Then
NotFnd = True
Fnd.Offset(, 1).Copy Target.Offset(, 2)
Exit For
End If
End If
Next Ws
If Not NotFnd Then MsgBox Target.Value & "not found"
Application.EnableEvents = True
End Sub
【问题讨论】: