Solved Trim (?) function (1 Viewer)

CosmaL

Registered User.
Local time
Today, 13:03
Joined
Jan 14, 2010
Messages
92
Dear friends,

i've got some cells with various string data.
All of them contain-for sure- a sequence of characters like H500, H501, H600, H603 etc

Is there any way to exctract only this sequence (H***) from each cell?

Thank you in advance!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:03
Joined
May 7, 2009
Messages
19,246
create a function in a Module:
Code:
Public Function fnH_Only(ByVal p As Variant) As Variant
Dim var, v
Dim ret As Variant
If IsNull(p) Then Exit Function
p = p & ""
With CreateObject("Vbscript.RegExp")
    .pattern = "\H\d{3}"
    .Global = True
    .ignorecase = True
    Set var = .Execute(p)
    For Each v In var
        ret = ret & v & ", "
    Next
    If Len(ret) Then
        ret = Left$(ret, Len(ret) - 2)
    End If
End With
fnH_Only = ret
End Function

example:

=fnH_Only(A1)
 

cheekybuddha

AWF VIP
Local time
Today, 11:03
Joined
Jul 21, 2014
Messages
2,321
Code:
=IF(Left(A1, 1) = "H", A1, "")
Place that in a new column and then drag down
 

ebs17

Well-known member
Local time
Today, 12:03
Joined
Feb 7, 2020
Messages
1,983
Code:
Public Function IsolateExpression(ByVal AnyString As String) As String
    Static oRegEx As Object
    If oRegEx Is Nothing Then Set oRegEx = CreateObject("Vbscript.RegExp")
    With oRegEx
        .Pattern = ".*(H\d{3}).*"
        IsolateExpression = .Replace(AnyString, "$1")
    End With
End Function

' -------------------------------------------------------------------------------------
? IsolateExpression("  H456 piece")
H456
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:03
Joined
May 7, 2009
Messages
19,246
just tested:

?IsolateExpression("All of them contain-for sure- a sequence of characters like H1")

Result:
All of them contain-for sure- a sequence of characters like H1
 

ebs17

Well-known member
Local time
Today, 12:03
Joined
Feb 7, 2020
Messages
1,983
All of them contain-for sure- a sequence of characters like H500, H501, H600, H603 etc
I read the assignment and I think @CosmaL means it.
All, for sure... words have meaning (to me).
 

cheekybuddha

AWF VIP
Local time
Today, 11:03
Joined
Jul 21, 2014
Messages
2,321
OK, but you can still do it using formulas (yes, much more complicated!) but no need to force macro-enabled workbook plus all the headaches that brings.

I misunderstood the requirement - due to language barrier it's not totally clear that OP is looking for cells containing Hnnn sequence only or strings within cells containing Hnnn sequence.

With your interpretation formula could be something like:
Code:
=IF(
  AND(
    SEARCH("*H???*", A1) > 0,
    ISNUMBER(
      MID(A1, SEARCH("*H???*", A1) + 1, 3)
    )
  ),
  MID(A1, SEARCH("*H???*", A1), 4),
  ""
)
(NB untested! :oops: )
 

cheekybuddha

AWF VIP
Local time
Today, 11:03
Joined
Jul 21, 2014
Messages
2,321
OK, opened excel!

This works better but will fail if there is another h before the Hnnn sequence:
Code:
=IF(
  ISERROR(
    IF(
      AND(
        SEARCH("H???", A1) > 0,
        ISNUMBER(
          MID(A1, SEARCH("H???", A1) + 1, 3) + 0
        )
      ),
      MID(A1, SEARCH("H???", A1), 4),
      ""
    )
  ),
  "",
  IF(
    AND(
      SEARCH("H???", A1) > 0,
      ISNUMBER(
        MID(A1, SEARCH("H???", A1) + 1, 3) + 0
      )
    ),
    MID(A1, SEARCH("H???", A1), 4),
    ""
  )
)
 

Users who are viewing this thread

Top Bottom