vba - VBA从不同位置提取相等的值
问题描述
这就是我目前在这个项目中想要实现的目标。在场景中,我在黄色位置标识了一个库存为零的项目,我需要搜索相同项目、类别但我不想看到的位置网络(数据是从主数据源中提取的)黄色位置,因为我已经知道黄色的库存为零
解决方案
@Raj 我不确定你的意思,但我尝试创建一个代码。
表 1
- 结构包括搜索引擎(黄色区域)和结果)
- 您只需在黄色字段上导入数据
- 代码运行的地方会擦除结果以带来新的结果。
表 2
结构(包括数据库)
尝试:
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
推荐阅读
- r - 在字符串的开头移动字符,在 R 中保持其他字符的位置不变
- javascript - MissingSchemaError:尚未为模型“用户”注册架构。尝试了stackOverflow上的所有答案
- system-verilog - super.build_phase() 有什么作用?
- openssl - 电子如何使用openssl?
- regex - VSCode:更改自定义TextMate语法中规则的优先级?
- css - 带文字的静态栏
- python - 如何使用 python 在 kafka 消费者中聚合 json 数据?
- amazon-web-services - 有没有办法使用 AWS step 函数中的状态输入来插入 OutputPath 的 JsonPath?
- python - MacOS Catalina 10.15.5 Flask-Mysqldb pip install 报错
- makefile - 带有`ifeq`的Makefile测试特定文件