简体   繁体   English

Excel Vba .find

[英]Excel Vba .find

Hi the names Mark and I am a new to VBA. 大家好,马克,我是VBA的新手。

I have two workbooks that contain data that needs to be compared. 我有两个工作簿,其中包含需要比较的数据。 I am currently using this code to do the comparisons. 我目前正在使用此代码进行比较。 It works and gives the results I require. 它可以正常工作,并提供我需要的结果。

What I am looking to add is the ability to search within a description in my second workbook range D:D and bring back matches to the cell Cells(rw, 4 ) in my active work book. 我要添加的功能是能够在第二个工作簿范围D:D的描述中进行搜索,并将匹配项带回到活动工作簿中的单元格Cells(rw,4 )。 This information would be placed in Cells(rw, 29 ) 此信息将放置在Cells(rw,29 )中

I have researched the find function but can not get it to work across two workbooks. 我研究了find函数,但无法在两个工作簿中使用它。 The challenge here being that the work book I search from or active workbooks name changes. 这里的挑战是我搜索的工作簿或活动的工作簿名称发生更改。

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

I believe you are having trouble using the FIND function because it is designed to find values/strings within a single cell. 我相信您在使用FIND函数时遇到了麻烦,因为该函数旨在在单个单元格内查找值/字符串。 You are attempting to search through an entire column. 您正在尝试搜索整个列。 A brute force way to solve your problem is to loop through your raw data column & check each cell for the desired match. 解决问题的一种蛮力方法是遍历原始数据列并检查每个单元格是否具有所需的匹配项。 I've included an example in your code below. 我在下面的代码中包含了一个示例。

I used the instr() function rather than find . 我使用了instr()函数而不是find They work much the same, but instr() is a bit more convenient for use in VBA. 它们的工作原理几乎相同,但是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

Hope this helps! 希望这可以帮助!

-Jacob -Jacob

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM