VBA code to find all cell in Col D in sheet 1 which contains cell value without match case from range A1 to A10 in Sheet 2

deba2020

New Member
Joined
Jan 8, 2020
Messages
26
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
I have 2 worksheets (Mysheet and Sheet1)
In Mysheet I have some partial text which the product description (column D) in Sheet1 has in it.
I want to loop through each items in Mysheet, locate each cell in Sheet1's Product Description (Col. D) that has that text, and then write the text from Mysheet in Sheet1's Col. C against each cell that contains that text.

I have this code but it is not giving desired results

Sub FindAndWrite()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim cell As Range, searchRange As Range, foundRange As Range

Set ws1 = ThisWorkbook.Sheets("Mysheet")
Set ws2 = ThisWorkbook.Sheets("Sheet1")
Set searchRange = ws1.Range("A1:A10")

For Each cell In searchRange
Set foundRange = ws2.Range("D:D").Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, searchdirection:=xlNext, MatchCase:=False)
If Not foundRange Is Nothing Then '
foundRange.Offset(0, -1).Value = "found"
End If
Next cell

End Sub

Please help.

Book12.xlsm
C
1Found Col
Sheet1


Book12.xlsm
A
2200 Silicone Fluid 200 cSt
Mysheet
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi @deba2020. Thanks for posting on MrExcel.

If more than one partial text matches in the sheet1 cell, then the macro adds it separated by a comma:

VBA Code:
Sub FindAndWrite()
  Dim ws1 As Worksheet, ws2 As Worksheet
  Dim c As Range, r As Range, f As Range, searchRange As Range
  Dim cell As String
  
  Set ws1 = ThisWorkbook.Sheets("Mysheet")
  Set ws2 = ThisWorkbook.Sheets("Sheet1")
  Set searchRange = ws1.Range("A1:A10")
  Set r = ws2.Range("D:D")
  
  r.Offset(, -1).ClearContents
  For Each c In searchRange
    Set f = r.Find(c.Value, , xlValues, xlPart, , , False)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        With f.Offset(0, -1)
          If .Value = "" Then .Value = c.Value Else .Value = .Value & ", " & c.Value
        End With
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
    End If
  Next
End Sub

---------------------------------------------​
For example, in cell D7 there are 2 texts, so in cell C7 both results were placed.
Dante Amor
CD
1
2text 1text 1
3text 1x text 1
4text 1text 1 x
5text 1x text 1 x
6text 2text 2
7text 1, text 2text 2 text 1
8
Sheet1


Dante Amor
A
1text 1
2text 2
3text 3
4text 4
5text 5
6text 6
7text 7
8text 8
9text 9
10text 10
Mysheet

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0
Solution
@DanteAmor - Hi Dante, just an FYI, although the microsoft web site gives the code you are using as their example the line below will fail if "f" is nothing.
The saving grace is that since we test for Is Nothing before getting there and we are only doing a find without a replace it will never be nothing at that point.

Rich (BB code):
Loop While Not f Is Nothing And f.Address <> cell
 
Last edited:
Upvote 0
Thank you very much for your valuable help and opinion.
The code worked wonderfully and the results are exactly as desired.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top