Automating Search/Find in VBA

Pobek

Board Regular
Joined
Jul 7, 2015
Messages
89
Imagine a Workbook with 5 Sheets. Each sheet has different countries in col A, the corresponding Capital City in B and say the population in C.

I am trying to code the use of FIND in VBA such that for any given list countries:

for i = 1 to no of countries to investigate

-search the entire workbook for country i
- if country i is found in the list
- bring in its associated capital and population in the next column of the same row of the country in question
- If not next i

next i

I find that the display of the find result restricts any form of loop action to take place hence intervening the macro on each iteration. Any suggestion?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
what about a UDF to vlookup the results? lets say you put in the country on A2 in sheet1 and wanted to search sheet2 to sheet5the formula would be in B2:


=udfWsVlookup(A2,A:B,2,FALSE,"Sheet2","Sheet5")

Cant't remember who to give the credit to for this UDF but it isn't mine


Code:
Public Function udfWsVlookup(rLV As Range, rTA As Range, lCI As Long, bRL As Boolean, ws1 As String, ws2 As String) As Variant
    Application.Volatile
    Dim w As Long, v As Variant
    udfWsVlookup = ""
    For w = Sheets(ws1).Index To Sheets(ws2).Index
        If Not IsError(Application.VLookup(rLV, Sheets(w).Range(rTA.Address), lCI, bRL)) Then
            v = Application.VLookup(rLV, Sheets(w).Range(rTA.Address), lCI, bRL)
            If Len(v) > 0 Then
                udfWsVlookup = v
                Exit For
            End If
        End If
    Next w
End Function
 
Upvote 0
what about a UDF to vlookup the results? lets say you put in the country on A2 in sheet1 and wanted to search sheet2 to sheet5the formula would be in B2:


=udfWsVlookup(A2,A:B,2,FALSE,"Sheet2","Sheet5")

Cant't remember who to give the credit to for this UDF but it isn't mine


Code:
Public Function udfWsVlookup(rLV As Range, rTA As Range, lCI As Long, bRL As Boolean, ws1 As String, ws2 As String) As Variant
    Application.Volatile
    Dim w As Long, v As Variant
    udfWsVlookup = ""
    For w = Sheets(ws1).Index To Sheets(ws2).Index
        If Not IsError(Application.VLookup(rLV, Sheets(w).Range(rTA.Address), lCI, bRL)) Then
            v = Application.VLookup(rLV, Sheets(w).Range(rTA.Address), lCI, bRL)
            If Len(v) > 0 Then
                udfWsVlookup = v
                Exit For
            End If
        End If
    Next w
End Function





Hi,

Thanks for that... I am away from the workbook at the miute but will certainly try and get back to you. Many thanks
 
Upvote 0

Forum statistics

Threads
1,214,849
Messages
6,121,922
Members
449,056
Latest member
denissimo

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