【问题标题】:Lookup with multiple criteria and preserve formatting使用多个条件查找并保留格式
【发布时间】: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

【问题讨论】:

    标签: excel vba lookup


    【解决方案1】:

    要保留格式,您应该像这样在目标上应用格式:

    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. 我会用变量替换硬编码的范围和工作表名称引用,以使子更灵活一点 2.我会开发子以支持多个单元格更改,如下所示:

    for each c in target
          if c.column = 4 then debug.print c.value
    next c
    

    【讨论】:

    • 我想我用帖子的标题误导了你......格式问题只与我可以用这段代码解决的字段“描述”有关。主要目标是根据 A16 中的文本将位置费率(Location1_Rate 到 Location7_Rate)查找与不同位置进行匹配。假设在 OUTPUT Sheet 中,如果我将 A16 下拉菜单中的 location 更改为 Location_2,则采用的速率应该是 MASTER 表中的 Location2_Rate。所以,这个问题就像使用多个条件进行查找一样好。
    • 好的。所以你的目标是填写描述和位置。当 D 列中的字段触发子时,项目代码旁边的速率。您有一个包含所有项目代码和位置的 MASTER 表。率。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多