Need help with VB Function

Discussion in 'Visual Basic ( VB )' started by gpabyc, Aug 26, 2009.

  1. gpabyc

    gpabyc New Member

    Joined:
    Aug 26, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    for some reason I cant get this to work. I need to remove all numbers from the end of a string leaving the ones at the beginning. So for example K1WIST-VED12 would = K1WIST-VED


    Code:
    Function StripNumber (GetComputerName As String) 
      Dim Str As String
        Dim i As Integer, b As Integer
     GetComputerName = Trim(GetComputerName) 
     ComputerNameLength = Len(GetComputerName)
        For i = ComputerNameLength To 1  Step -1 
            If IsNumeric(Mid(GetComputerName, i, 1)) Then 
                Str = (Left(GetComputerName, ComputerNameLength - b) 
                b = (b + 1) 
            Else StripNumber = Trim(Str)
            End If 
       Next i
      Return StripNumber = Trim(Str)
     End Function
     
    Last edited by a moderator: Aug 27, 2009
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    OK. I have corrected the code for you. Here it is :

    Code:
    Function StripNumber (GetComputerName)
        Dim Str As String
        Dim i As Integer, b As Integer
        b = 1
        GetComputerName = Trim(GetComputerName) 
        ComputerNameLength = Len(GetComputerName)
        For i = ComputerNameLength To 1  Step -1 
            If IsNumeric(Mid(GetComputerName, i, 1)) Then 
                Str = Left(GetComputerName, ComputerNameLength - b) 
                b = b + 1
            Else
                Exit For
            End If
       Next
       StripNumber = Trim(Str)
    End Function
    
    It should work fine.


    The code is pretty self-explanatory. But I would just explain one point :
    in the else condition of the If block inside the loop, you should use "Exit For" so that the control jumps out of the for loop when thefirst non-numeric character (from end) is encountered.
     
    Last edited: Aug 27, 2009
  3. gpabyc

    gpabyc New Member

    Joined:
    Aug 26, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thank you for the reply but Im getting a Microsoft VBScript compilation error: Expected end of statement for the As String and As Integer statements when i remove them that error clears but returns blank
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    You should have mentioned that you are using VBScript.
    The code I gave would compile fine in VB6.

    Anyway, here is the VBScript Code:
    Code:
    Function StripNumber (GetComputerName)
        Dim Str,i,b
        b = 1
        GetComputerName = Trim(GetComputerName)
        ComputerNameLength = Len(GetComputerName)
        For i = ComputerNameLength To 1  Step -1
            If IsNumeric(Mid(GetComputerName, i, 1)) Then
                Str = Left(GetComputerName, ComputerNameLength - b)
                b = b + 1
            Else
                Exit For
            End If
       Next
       If Str = "" And b = 1 Then
          Str = GetComputerName
       End If
       StripNumber = Trim(Str)
    End Function
    
    To try the above func, here is a test program:
    Code:
    Dim mStr
    mStr = InputBox("Enter a test value")
    Msgbox(StripNumber(mStr))
    WScript.quit
    
    Function StripNumber (GetComputerName)
        Dim Str,i,b
        b = 1
        GetComputerName = Trim(GetComputerName)
        ComputerNameLength = Len(GetComputerName)
        For i = ComputerNameLength To 1  Step -1
            If IsNumeric(Mid(GetComputerName, i, 1)) Then
                Str = Left(GetComputerName, ComputerNameLength - b)
                b = b + 1
            Else
                Exit For
            End If
       Next
       If Str = "" And b = 1 Then
          Str = GetComputerName
       End If
       StripNumber = Trim(Str)
    End Function
    

    The previous code had a very minor bug which return "blank" only when the input text was *purely* alphabetical.
    Hope this one is alright.

    Tell me if it works .. :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice