简体   繁体   中英

vba to search cell values in another workbook's column

I have a column "F" in workbook1 containing some values (obtained after using some excel formulas to extract and concatenate from other columns) like blah-rd1 blah-rd5 blah-rd6 blah-rd48do I want to do this blah-rd100 etc

I have another column "D" in workbook2 containing values like rndm-blah-rd1_sgjgs hjdf-blah-rd5_cnnv sdfhjdf-blah-rd100_cfdnnv ect

Basically "Blah-rdxx" is always present alongwith other strings in D column of workbook2

Now, what I want to do is - If value in D column of workbook2 contains value of F column of workbook1 Then copy corresponding value of S column of workbook2 in H column of workbook1 (5th column)

This is where I have reached so far but it doesnt copy anything probably coz there is some problem and the outer loop is not iterating, I tried following solution Nested For Next Loops: Outer loop not iterating and added n counter but still outer loop doesn't iterate -

Sub findandcopy()
Dim r As Range
Dim f As Range

Dim i As Long
Dim j As Long
Dim w1 As Worksheet
Dim w2 As Worksheet
Dim n As Integer

Set w1 = Workbooks("Book1.xlsm").Worksheets("sheet1")
Set w2 = Workbooks("Book2.xlsx").Worksheets("sheet1")


n = 0
For i = 1 To w2.Cells(Rows.Count, 1).End(xlUp).Row
For j = 1 To w1.Cells(Rows.Count, 1).End(xlUp).Row + n

If InStr(1, w2.Cells(i, 1).Value, w1.Cells(j, 3).Value) > 0 Then

w2.Cells(i, 2).Copy (w2.Cells(j, 5))
Exit For
n = n + 1
End If

Next j
Next i

End Sub

Try this


Option Explicit

Public Sub FindAndCopy()

    Const F = "F"
    Const D = "D"
    Const H = 2
    Const S = 15

    Dim ws1 As Worksheet:   Set ws1 = Workbooks("Book1.xlsm").Worksheets("Sheet1")
    Dim ws2 As Worksheet:   Set ws2 = Workbooks("Book2.xlsm").Worksheets("Sheet1")
    Dim lr1 As Long:        lr1 = ws1.Cells(ws1.Rows.Count, F).End(xlUp).Row
    Dim lr2 As Long:        lr2 = ws2.Cells(ws2.Rows.Count, D).End(xlUp).Row

    Dim itm1 As Range, itm2 As Range

    Application.ScreenUpdating = False
    For Each itm2 In ws2.Range(ws2.Cells(1, D), ws2.Cells(lr2, D))      'Book2
        For Each itm1 In ws1.Range(ws1.Cells(1, F), ws1.Cells(lr1, F))  'Book1
            If Not IsError(itm1) And Not IsError(itm2) Then
                If InStr(1, itm2.Value2, itm1.Value2) > 0 Then
                    itm1.Offset(, H).Formula = itm2.Offset(, S).Formula 'Book1.H = Book2.S
                    Exit For
                End If
            End If
        Next
    Next
    Application.ScreenUpdating = True
End Sub

The original code, with explanations of functional issues:


Sub findandcopy()
 Dim w1 As Worksheet, w2 As Worksheet
 Dim i As Long, j As Long, n As Integer

 Set w1 = Workbooks("Book1.xlsm").Worksheets("sheet1")
 Set w2 = Workbooks("Book2.xlsx").Worksheets("sheet1")

 n = 0
 For i = 1 To w2.Cells(Rows.Count, 1).End(xlUp).Row       'for each used cell in w2.colA
   For j = 1 To w1.Cells(Rows.Count, 1).End(xlUp).Row + n 'for each used cell in w1.colA

    'Find the text from w1.colC (current w1 row), within cell in w2.colA (current w2 row)
     If InStr(1, w2.Cells(i, 1).Value, w1.Cells(j, 3).Value) > 0 Then

      'If found then copy cell in w2.colB into cell in w2.colE (current w2 row)
       w2.Cells(i, 2).Copy (w2.Cells(i, 5))

       Exit For    'this exits the inner For loop

       n = n + 1   'this would jump over the next cell(s) in w1, but never executes
     End If
   Next j
 Next i
End Sub

  • The missing indentation makes it hard to follow
  • There are unused variables (r, f), and w1 / w2 names can mean Workbook, or Worksheet
  • "Option Explicit" should be used at the top of every module
  • The code doesn't handle cells with errors
    • #N/A, #VALUE!, #REF!, #DIV/0!, #NUM!, #NAME?, or #NULL!

If you'd like a more detailed review of the code, once it's fixed you can post it on Code Review

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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