我有一张MASTER表,其中包含物品代码(第B栏),说明(第C栏),单位(第D栏),地点1号税(第E栏),地点2号税(第F栏),地点3号税(第G栏) ),Location4_Rate(Col.H),Location5_Rate(Col。I),Location6_Rate(Col.J),Location7_Rate(Col。K)
我使用配置如下的OUTPUT表
位置选择(Location_1到Location_7)作为下拉菜单:A16
物品代码:Col D.(手动输入)
描述:上校F
数量:Col。G
单位:上校H.
率:上校I
金额:上校J
每当我在OUTPUT表中输入项目代码(Col.D)时,项目的描述以及相应位置的费率应分别自动出现在Col.F和Col.I的相应单元格中。第一个物品代码以单元格D20开头。
因为,我的目标是保留OUTPUT表格中的格式,我使用VBA代码作为描述字段。我在VBA代码中成功返回单个位置的速率(Location1_Rate)。但是,我无法修改获取不同位置的速率的代码(基于单元格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
答案 0 :(得分:0)
要保留格式,您应该在目标上应用格式,如下所示:
With Target.Offset(, 2)
.Font.Size = 12
.Font.Bold = True
whatever
不幸的是,VBA无法使用格式画家,因此如果您不了解格式(或希望保持应用程序的灵活性),则需要从源单元格中复制相关设置,例如
With Target.Offset(, 2)
.Font.Size = Fnd.Offset(0, 1).Font.Size
.Font.Bold = Fnd.Offset(0, 1).Font.Bold
whatever
格式化有很多属性,找到相关的好运。
编辑:试一试:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Fnd As Range
Dim item_row As Long
If Target.CountLarge > 1 Then Exit Sub
If Not Target.Column = 4 Then Exit Sub
Set Fnd = Sheets("MASTER").Range("B:B").Find(Target.Value, , , xlWhole, , , False, , False) ' item code
If Fnd Is Nothing Then
MsgBox "Item not found"
Exit Sub
Else
Fnd.Offset(, 1).Copy Target.Offset(, 2) ' desc
item_row = Fnd.Row ' row of item
Set Fnd = Sheets("MASTER").Range("E1:K1").Find(Cells("A16").Value, , , xlWhole, , , False, , False) ' loc name
If Fnd Is Nothing Then
MsgBox "Location not found"
Else
Sheets("MASTER").Cells(item_row, Fnd.Column).Copy Target.Offset(0, 5)
End If
End If
End Sub
注意:1。我会用变量替换硬编码的range和sheetname引用,以使sub更灵活一些 2.我会开发sub以支持超过1个单元格的更改,如下所示:
for each c in target
if c.column = 4 then debug.print c.value
next c