Mark
Mark

Reputation: 45

Excel Vba .find

Hi the names Mark and I am a new to 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. This information would be placed in Cells(rw, 29)

I have researched the find function but can not get it to work across two workbooks. 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

Upvotes: 2

Views: 1534

Answers (1)

Jacob
Jacob

Reputation: 369

I believe you are having trouble using the FIND function because it is designed to find values/strings within a single cell. 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. They work much the same, but instr() is a bit more convenient for use in 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

Upvotes: 1

Related Questions