VBA从不同位置拉取相等值

时间:2018-10-18 18:38:23

标签: vba

这是我目前在此项目中想要实现的目标。场景中,我在黄色位置上标识了一个零库存的项目,我需要搜索相同项目,类别的位置网络(数据是从主数据源中提取的),但我不想看到黄色的位置,因为我已经知道黄色的库存为零

enter image description here

1 个答案:

答案 0 :(得分:0)

@Raj我不确定您的意思,但我尝试创建代码。

第1张纸

enter image description here

  1. 结构包括搜索引擎(黄色区域)和结果)
  2. 您只需在黄色字段上导入数据
  3. 运行代码的地方会擦除结果以带来新的结果。

第2张纸

enter image description here

结构(包括数据库)

尝试:

Option Explicit

Sub Test()

    Dim LastRow2 As Long
    Dim i As Long
    Dim Mat_Cat_Loc As String
    Dim LastRow1 As Long

    With Sheet1

        If .Range("B2").Value = "" Or .Range("B3").Value = "" Or .Range("B4").Value = "" Then '<= if some search part missing end sub
            Exit Sub
        End If

        Mat_Cat_Loc = .Range("B2").Value & .Range("B3").Value & .Range("B4").Value ' <= Create Mat_Cat_Loc for the search product

        LastRow1 = .Range("A" & Rows.Count).End(xlUp).Row

        .Range("A8:D" & LastRow1).Clear '<= Clear range from the previous search

    End With

    With Sheet2

        LastRow2 = .Range("A" & Rows.Count).End(xlUp).Row

        For i = 3 To LastRow2 ' <= Loop in Sheet2 and if Mat_Cat_Loc (From the search) does not match Mat_Cat_Loc (From the data does not copy the line to Sheet1 Rsult Table)
            If (.Range("D" & i).Value <> Mat_Cat_Loc) And (.Range("C" & i).Value) <> Sheet1.Range("B4").Value Then
                With Sheet1
                    LastRow1 = .Range("A" & Rows.Count).End(xlUp).Row
                    .Range("A" & LastRow1 + 1).Value = Sheet2.Range("A" & i).Value
                    .Range("B" & LastRow1 + 1).Value = Sheet2.Range("B" & i).Value
                    .Range("C" & LastRow1 + 1).Value = Sheet2.Range("C" & i).Value
                    .Range("D" & LastRow1 + 1).Value = Sheet2.Range("D" & i).Value
                End With
            End If
        Next i
    End With

End Sub