Search for String and Copy in VBA

wibni

New Member
Joined
Jun 15, 2011
Messages
33
Hello,
I'm using Excel 2003.
In my table below I'm trying to copy the description from column B to column K (starting at row 9).
I'm only looking at the rows that have 'Site' in colum A.
The way it finds the correct description is by looking for the string in column J (starting at row 9).
For example in Cell J9 the number is '055'.
'055' is found on row 2, therfore get the description from B2 and copy to K9.
If there are only characters like in J16, simply copy J16 to K16.

I currently have it working in a vlookup but now require the lookup table to be on the same sheet in the format shown.
My vlookup formula is like this:
=IF(ISNUMBER(J9+0),VLOOKUP(J9,Rigs!A$1:B$18,2,FALSE),J9)

How can I achieve this in VBA?

Excel Workbook
ABCDEFGHIJK
1LocationDescSum Rigs
2SiteGGM6008028038055066073
3SiteNMM8005011021024032042048049
4OfficeMWA14008028038055066073005011
5OfficeKNC14008028038055066073005011
6
7
8RigSiteSite
9055GGM
10066GGM
11073GGM
12008GGM
13028GGM
14055GGM
15066GGM
16NMMNMM
17005NMM
Sheet1
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Code:
Sub Site_Lookup()
    
    Dim rngSites As Range, rngLookup As Range, cell As Range, found As Range
    
    Set rngSites = Range("D2:K3")
    Set rngLookup = Range("J9:J17")
    
    For Each cell In rngLookup
        If cell.Value <> "" Then
            If IsNumeric(cell) Then
                Firstfound = ""
                Set found = rngSites.Find(cell.Value, , xlValues, xlWhole, xlByRows, xlNext, False)
                If Not found Is Nothing Then
                    ' Site found
                    cell.Offset(, 1).Value = Range("B" & found.Row).Value
                Else
                    ' No Match
                    cell.Offset(, 1).Value = "N\A"
                End If
            Else
                ' Text
                cell.Offset(, 1).Value = cell.Value
            End If
        End If
    Next cell
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,752
Members
452,940
Latest member
rootytrip

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