using vba to extract characters within a string

johnmerlino

Board Regular
Joined
Sep 21, 2010
Messages
94
Hey all,

I have a cell like this:

Code:
_AS THE UNKNOWN SPOUSE OF KATARINA ASKI,

I have this:

Code:
...code
unknown = InStr(Range("K" & i), "UNKNOWN SPOUSE OF ")

...code
 ElseIf unknown <> 0 Then
            LValue = Right(Range("K" & i), Len(Range("K" & i)) - unknown)
 End If
...code

That above statement returns:

Code:
S THE UNKNOWN SPOUSE OF KATARINA ASKI,

Rather than
Code:
KATARINA ASKI

thanks for response
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Place this bit of code in yours:
Code:
MsgBox Unknown
It will tell you the number of characters you are subtracting which I suspect is too small..
 
Upvote 0
Try adding
Rich (BB code):
unknown = unknown + 18 'Oops!

It'd be best if
Rich (BB code):
Dim f As String
f = "UNKNOWN SPOUSE OF "
 
...code
unknown = InStr(Range("K" & i), f) + Len(f)
 
...code
 ElseIf unknown <> 0 Then
            LValue = Right(Range("K" & i), Len(Range("K" & i)) - unknown)
 End If
...code
 
Last edited:
Upvote 0
If you want to find statements other than "UNKNOWN SPOUSE OF ", just use the method of Inputbox.

I've noticed in ur result, the punctuation is missing.
I think filter method would work the best in that case.
 
Upvote 0
Try adding
Rich (BB code):
unknown = unknown + 18 'Oops!

It'd be best if
Rich (BB code):
Dim f As String
f = "UNKNOWN SPOUSE OF "
 
...code
unknown = InStr(Range("K" & i), f) + Len(f)
 
...code
 ElseIf unknown <> 0 Then
            LValue = Right(Range("K" & i), Len(Range("K" & i)) - unknown)
 End If
...code

Thanks for response. This gives me invalid procedure call or argument.
 
Upvote 0
Could I see your whole code please?
and where has the error occurred?

This is whole code:

Code:
Sub Inspect()
 
Dim RENums As Object
Dim RENums2 As Object
Dim LValue  As String
Dim LValue2  As String
Dim f As String
f = "UNKNOWN SPOUSE OF "
 
Set RENums = CreateObject("VBScript.RegExp")
Set RENums2 = CreateObject("VBScript.RegExp")
 
 
RENums.Pattern = "DEFENDANT"
RENums2.Pattern = "FORECLOSURE"
 
 
  Dim lngLastRow As Long
  lngLastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
  
 
  Dim i
 
  For i = 1 To lngLastRow
 
    If RENums2.test(Range("F" & i).Value) Then
 
      If RENums.test(Range("J" & i).Value) Then
   
          pos = InStr(Range("G" & i), " V ")
         
          pos2 = InStr(Range("G" & i), " VS ")
          
          pos3 = InStr(Range("G" & i), " V ESTATE OF ")
 
          dbspace = InStr(Range("K" & i), "  ")
          

          unknown = InStr(Range("K" & i), f) + Len(f)
   
          If pos3 <> 0 Then
             LValue2 = Right(Range("G" & i), Len(Range("G" & i)) - pos * 2)
          ElseIf pos <> 0 Then
             LValue2 = Right(Range("G" & i), Len(Range("G" & i)) - pos - 2)
          ElseIf pos2 <> 0 Then
            LValue2 = Right(Range("G" & i), Len(Range("G" & i)) - pos - 2)
          End If
 
          If dbspace <> 0 Then
            LValue = Range("K" & i)
          ElseIf unknown <> 0 Then
            LValue = Right(Range("K" & i), Len(Range("K" & i)) - unknown)
          End If
 
          
            schr = Right(LValue, 1)
            If schr = "_" Then
              With WorksheetFunction
               Range("N" & i).Value = Trim(.Substitute(LValue, "_", ""))
              End With
            Else
              Range("N" & i).Value = Trim(LValue)
            End If
            Range("O" & i).Value = Trim(LValue2)
          
           
      End If
    End If
 
  Next i
End Sub

Thanks for response
 
Upvote 0
Could it be because of this?

Rich (BB code):
LValue2 = Right(Range("G" & i), Len(Range("G" & i)) - pos * 2)

it would return subscript out of range error.. so I doubt it..

It very well could be from If statements where you call the objects.
Other than that I don't see any problems with it.

It'd be best if you 'declare/dim' variables which have not been. Altho that doesn't make a huge difference.

and please change unknown initialization to
Rich (BB code):
unknown = InStr(Range("K" & i), f) + Len(f) - 1
 
Upvote 0

Forum statistics

Threads
1,224,570
Messages
6,179,610
Members
452,931
Latest member
The Monk

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