Excel Vba .find

大家好,Mark和我是VBA的新手。

我有两个包含需要比较的数据的工作簿。 我正在使用这个代码做比较。 它的工作,并给出我需要的结果。

我想要添加的是能够在我的第二个工作簿范围D:D中的描述中进行search,并将匹配返回到我的活动工作簿中的单元格(rw,4 )。 这个信息将被放置在Cells(rw,29

我已经研究了查找function,但不能让它在两个工作簿上工作。 这里面临的挑战是我从中search的工作簿或活动工作簿名称的变化。

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函数时遇到了麻烦,因为它是为了在单个单元格中查找值/string而devise的。 您正试图search整个列。 解决您的问题的一个蛮力方法是循环通过您的原始数据列和检查每个单元格所需的匹配。 我在下面的代码中包含了一个例子。

我用instr()函数而不是find 。 它们的工作方式instr() ,但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 

希望这可以帮助!

-Jacob