excel - 单元格匹配多个或字符串然后复制
问题描述
我是初学者。
我希望宏在每列中部分匹配“别克”、“雪佛兰”或“庞蒂亚克”。如果有的话,每个列将只有一个匹配项。
- D 列“SheetJS”中的匹配项应复制到“Sheet1”中的 AA 列。
- E 列“SheetJS”中的匹配项应复制到“Sheet1”中的 AH 列。
- F 列(“SheetJS”)中的匹配项应复制到“Sheet1”中的 AL 列。
我有其他范围和其他词要匹配,但如果可以让这段代码工作,我应该能够编写剩下的东西。
我不断收到一条错误消息:
编译错误:预期:Then 或 GoTo
我不知道如何解决这个问题。
Sub Extract_Data_Buick2()
For Each cell In Sheets("SheetJS").Range("D1:D200")
matchrow = cell.Row
If instr ("*Buick*", cell.value)> 0 Or_
instr ("*Chevrolet*", cell.value)> 0 Or_
instr ("*Pontiac*", cell.value)> 0 Or_ Then
Sheets("Sheet1").Range("AA" & matchrow).Value = cell.Value
End If
Next
For Each cell In Sheets("SheetJS").Range("E1:E200")
matchrow = cell.Row
If instr ("*Buick*", cell.value)> 0 Or_
instr ("*Chevrolet*", cell.value)> 0 Or_
instr ("*Pontiac*", cell.value)> 0 Or_ Then
Sheets("Sheet1").Range("AH" & matchrow).Value = cell.Value
End If
Next
For Each cell In Sheets("SheetJS").Range("F1:F200")
matchrow = cell.Row
If instr ("*Buick*", cell.value)> 0 Or_
instr ("*Chevrolet*", cell.value)> 0 Or_
instr ("*Pontiac*", cell.value)> 0 Or_ Then
Sheets("Sheet1").Range("AL" & matchrow).Value = cell.Value
End If
Next
End Sub
解决方案
您可以这样做 - 删除重复项并将其放在单独的 Sub 中。
Sub Extract_Data_Buick2()
CopyMatches Sheets("SheetJS").Range("D1:D200"), "AA"
CopyMatches Sheets("SheetJS").Range("E1:E200"), "AH"
CopyMatches Sheets("SheetJS").Range("F1:F200"), "AL"
End Sub
Sub CopyMatches(rng As Range, DestCol as string)
Dim v, e, c As Range
For Each c In rng.Cells
v = c.Value
For Each e In Array("Buick", "Chevrolet", "Pontiac")
If Instr(v, e) > 0 Then
Sheets("Sheet1").Range(DestCol & c.Row).Value = v
Exit For 'stop checking for this cell
End If
Next e
Next c
End Sub
推荐阅读
- c# - PayPal响应式结账?
- spring - 项目中没有@EnableScheduling注解的调度器是如何运行的
- go - Vscode golang 扩展在格式上需要很长时间
- javascript - 如果本地存储中存在该评论的 ID,如何禁用该评论的投票按钮?
- javascript - 我如何通过将 AppMetrica 添加到 react-native 项目来修复错误
- c++ - src/bsoncxx/CMakeLists.txt 中的 CMake 错误 - Mongodb Cxx 驱动程序
- java - Java 基本运算符返回类型
- javascript - 如何在我的 javascript Web 应用程序的数据库层中为类函数编写单元测试?
- c# - GetResponseAsync png 链接 url - 参数无效
- python - 在python中编辑文本文件中特定行的一部分