VBA: Search an API response for any of 10 words. If word is found, display the 20 characters before & after

yits05

Board Regular
Joined
Jul 17, 2020
Messages
56
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have reached my VBA abilities in trying to figure this out, and was hoping for some help. I have a script which calls an API and returns a bunch of text, stored in a string variable. I was wondering if there is any way in which I can VBA search the string for any of a list of keywords, and if one is found, for it to return in a cell the 20 characters before & after the keyword appears?

There is also a chance that multiple keywords will appear in the string, so I would want them added vertically in the same row, ie:

hsfdhfosjglksjfgKEYWORD1hgwlkejtghljkhwetlgewjf'lkwqg]pojqj34pi5jgKEYWORD2hjfdk.hwrlhjwlghjqkdwjg...

Much appreciated!
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try this - implemented as a test routine and a search routine which you can call from your code.
VBA Code:
Public Sub Test()

    Dim APItext As String
    Dim keywords As Variant
    
    APItext = "AAAAAAhsfdhfosjglksjfgKEYWORD1hgwlkejtghljkhwetlgBBBBBCCCCCCewjflkwqg]pojqj34pi5jgKEYWORD2hjfdk.hwrlhjwlghjqkdwjgDDDDDD"
    
    keywords = Array("KEYWORD1", "KEYWORD2", "KEYWORD3")
    
    Keywords_Search APItext, keywords, Range("A2")
        
End Sub


Public Sub Keywords_Search(APItext As String, keywords As Variant, destCell As Range)

    Dim keyword As Variant
    Dim p1 As Long, p2 As Long
    Dim colOffset As Long
    
    colOffset = 0
    For Each keyword In keywords
        p2 = InStr(1, APItext, keyword, vbBinaryCompare)
        If p2 > 0 Then
            p1 = p2 - 20
            If p1 < 1 Then p1 = 1
            destCell.Offset(, colOffset).Value = Mid(APItext, p1, p2 - p1 + Len(keyword) + 20)
            colOffset = colOffset + 1
        End If
    Next
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,749
Members
449,050
Latest member
excelknuckles

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