Consulting

Results 1 to 4 of 4

Thread: Deleting text from right side of an individual cell

  1. #1

    Deleting text from right side of an individual cell

    I'm attempting to clear text out of the right hand side of a cell. The cell looks like this.


    "431: were staged"

    The numerical value in this case 431 can be as large as 6 digits or as small as 2. I need to delete the ": were staged" text off the number.

    I've tried using

    [VBA]Dim x As String
    Dim y As String
    Dim z As String

    Dim trimstring

    x = Range("b1")


    y = Right(x, 13)

    z = x - y

    End Sub[/VBA]

    without success

    I've also tried the .characters function but I can't get it to count from the right.

    Any help would be much appreciated!

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]
    Function NumberPart(aString As String) As Long
    Dim s As String, i As Integer, mc As String
    For i = 1 To Len(aString)
    mc = Mid(aString, i, 1)
    If Asc(mc) >= 48 And Asc(mc) <= 57 Then s = s & mc
    Next i
    NumberPart = CLng(s)
    End Function

    Sub Test_Numberpart()
    Dim s As String
    s = "A5BC123"
    Debug.Print s, NumberPart(s)
    End Sub[/VBA]

  3. #3
    Thanks so much for your help Kenneth!! It worked great!

    [VBA]Function NumberPart(aString As String) As Long
    Dim s As String, i As Integer, mc As String
    For i = 1 To Len(aString)
    mc = Mid(aString, i, 1)
    If Asc(mc) >= 48 And Asc(mc) <= 57 Then s = s & mc
    Next i
    NumberPart = CLng(s)
    Range("a2") = s
    End Function

    Sub Test_Numberpart()
    Dim s As String
    s = Range("a1")
    Debug.Print s, NumberPart(s)


    End Sub[/VBA]

  4. #4
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    You can use the Val() function to extract the number from the beginning of a string:
    [VBA]
    Function numPart(strInput As String) As Long
    numPart = Val(strInput)
    End Function[/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •