gpt4 book ai didi

vba - Excel Vba .find

转载 作者:行者123 更新时间:2023-12-04 20:44:46 27 4
gpt4 key购买 nike

大家好,马克,我是 VBA 的新手。

我有两个工作簿,其中包含需要比较的数据。我目前正在使用此代码进行比较。它有效并给出了我需要的结果。

我希望添加的是在我的第二个工作簿范围内的描述中搜索的能力 D:D 并将匹配项带回单元格 我的事件工作簿中的单元格(rw,4 )。此信息将放在 单元格(rw,29 )

我研究了查找 功能,但无法让它跨两个工作簿工作。这里的挑战是我搜索的工作簿或事件工作簿名称发生了变化。

Sub VlookUpExampleDifferBooks()

'This example look up table in different book and sheet (TABLE 1 - ActiveSheet, TABLE 2 - CMF.xlxs and sheet1)
'Validate_Down Macro

Dim LastRow As Long
Dim rw As Long
Dim mx As Integer

Application.ScreenUpdating = False 'Find Last Row
LastRow = Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row

For rw = 11 To LastRow ' Loop until rw = Lastrow
Cells(rw, 27) = "'" & Cells(rw, 4)
Cells(rw, 28) = "'" & Cells(rw, 2)
Cells(rw, 25) = Application.VLookup(Cells(rw, 27), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("A:D"), 3, False) ' Vlookup function
Cells(rw, 26) = Application.VLookup(Cells(rw, 28), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("B:D"), 2, False) ' Vlookup function
Cells(rw, 20) = Application.VLookup(Cells(rw, 18), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("C:D"), 2, False) ' Vlookup function
Cells(rw, 19) = Application.VLookup(Cells(rw, 18), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("C:E"), 3, False) ' Vlookup function

If IsError(Cells(rw, 25)) Then Cells(rw, 25) = ""
If IsError(Cells(rw, 26)) Then Cells(rw, 26) = ""
If Cells(rw, 25) <> Cells(rw, 26) Then Cells(rw, 18) = Cells(rw, 25) & "/" & Cells(rw, 26)
If Cells(rw, 25) = Cells(rw, 26) Then Cells(rw, 18) = "'" & Cells(rw, 25)
If Cells(rw, 25) <> Cells(rw, 26) And Cells(rw, 26) = "" Then Cells(rw, 18) = "'" & Cells(rw, 25)
If Cells(rw, 25) <> Cells(rw, 26) And Cells(rw, 25) = "" Then Cells(rw, 18) = "'" & Cells(rw, 26)
If IsError(Cells(rw, 20)) Then Cells(rw, 20) = ""
If IsError(Cells(rw, 19)) Then Cells(rw, 19) = ""

Next

最佳答案

我相信您在使用 FIND 时遇到了问题函数,因为它旨在在单个单元格中查找值/字符串。您正在尝试搜索整个列。解决您的问题的蛮力方法是遍历您的原始数据列并检查每个单元格以获得所需的匹配。我在下面的代码中包含了一个示例。

我用了instr()函数而不是 find .它们的工作方式大致相同,但 instr()在 VBA 中使用更方便一些。

Sub VlookUpExampleDifferBooks()

'This example look up table in different book and sheet (TABLE 1 - ActiveSheet, TABLE 2 - CMF.xlxs and sheet1)
'Validate_Down Macro

Dim LastRow As Long
Dim rw As Long
Dim mx As Integer

Application.ScreenUpdating = False 'Find Last Row
LastRow = Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row

For rw = 11 To LastRow ' Loop until rw = Lastrow
Cells(rw, 27) = "'" & Cells(rw, 4)
Cells(rw, 28) = "'" & Cells(rw, 2)
Cells(rw, 25) = Application.VLookup(Cells(rw, 27), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("A:D"), 3, False) ' Vlookup function
Cells(rw, 26) = Application.VLookup(Cells(rw, 28), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("B:D"), 2, False) ' Vlookup function
Cells(rw, 20) = Application.VLookup(Cells(rw, 18), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("C:D"), 2, False) ' Vlookup function
Cells(rw, 19) = Application.VLookup(Cells(rw, 18), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("C:E"), 3, False) ' Vlookup function


'Loop through all cells in column D and check each one for the value in cells(rw,4)
For Each testcell In Workbooks("CMF Export.xlsx").Sheets("Sheet1").Range("D:D")
If InStr(1, testcell.Value, Cells(rw, 4).Value, vbTextCompare) <> 0 Then
Cells(rw, 29).Value = Cells(rw, 4).Value
Exit For

End If
Next testcell

If IsError(Cells(rw, 25)) Then Cells(rw, 25) = ""
If IsError(Cells(rw, 26)) Then Cells(rw, 26) = ""
If Cells(rw, 25) <> Cells(rw, 26) Then Cells(rw, 18) = Cells(rw, 25) & "/" & Cells(rw, 26)
If Cells(rw, 25) = Cells(rw, 26) Then Cells(rw, 18) = "'" & Cells(rw, 25)
If Cells(rw, 25) <> Cells(rw, 26) And Cells(rw, 26) = "" Then Cells(rw, 18) = "'" & Cells(rw, 25)
If Cells(rw, 25) <> Cells(rw, 26) And Cells(rw, 25) = "" Then Cells(rw, 18) = "'" & Cells(rw, 26)
If IsError(Cells(rw, 20)) Then Cells(rw, 20) = ""
If IsError(Cells(rw, 19)) Then Cells(rw, 19) = ""

Next

希望这可以帮助!

-雅各布

关于vba - Excel Vba .find,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21318301/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com