+ Reply to Thread
Results 1 to 4 of 4

Function works but I get a run time error 13.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-16-2012
    Location
    London, England
    MS-Off Ver
    Excel mac 2011
    Posts
    238

    Function works but I get a run time error 13.

    Hi,

    This is a function to add the ascii values of a string to give a single value. Initially I wrote this as a Sub routine and it worked fine, but when making it into a function, I get this error. From a little research it appears that you can not use a string as an arguement for a function call, yet that defeats the object of this particular function.

    The intention of this function is that it works on a name in a given cell and the value (an integar) that is produced is then displayed in another cell, or if using it as a formula, the cell the formula is in.

    I simple wrote the work "Hello" in cell A1 the ascii values of these added ignoring any spaces give 500 which is correct.

    Option Explicit
    Sub Test()
        TestNum ("A1")
    End Sub
    
    Function TestNum()
        Dim s As String
        Dim i As Integer
        Dim x As Integer
        Dim y As Integer
        
        s = Range("A1")
        y = 0
        For i = 1 To Len(s)
            x = Asc(Mid$(s, i, 1))
            If x <> 32 Then
                y = y + x
            End If
        Next i
        Range("D1") = y
    End Function
    Any help would be appreciated

    Rob

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Function works but I get a run time error 13.

    Rob

    Your function doesn't take any arguments, it also doesn't return anything.

    Here's a function that can be used to add the ASCII values of a string.
    Function SumASCII(txt As String)
    Dim i As Integer
    Dim x As Integer
    Dim y As Integer
    
    
        For i = 1 To Len(txt)
            x = Asc(Mid$(txt, i, 1))
            If x <> 32 Then
                y = y + x
            End If
        Next i
    
        SumASCII = y
    
    End Function
    You would call it using a formula in a cell like this.
    Formula: copy to clipboard

    =SumASCII("Test")
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    02-16-2012
    Location
    London, England
    MS-Off Ver
    Excel mac 2011
    Posts
    238

    Re: Function works but I get a run time error 13.

    Thank you Norie, that is excellent, I actually came up with this after discovering the debug command, it's almost identical, but it is good to get verification.
    Option Explicit
    Sub Test()
        TestNum (Range("A1"))
    End Sub
    
    Function TestNum(s As String)
        Dim i As Integer
        Dim x As Integer
        Dim y As Integer
        
        y = 0
        For i = 1 To Len(s)
            x = Asc(Mid$(s, i, 1))
            If x <> 32 Then
                y = y + x
                Debug.Print "The value of variable I is: " & i
                Debug.Print "The value of variable S is: " & s
                Debug.Print "The value of variable X is: " & x
                Debug.Print "The value of variable Y is: " & y
            End If
        Next i
        Range("D1") = y
    End Function

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Function works but I get a run time error 13.

    The function still isn't actually returning anything.

    You could change it to this and the value the function returns will be put in D1.
    Sub Test()
        Range("D1") =TestNum (Range("A1"))
    End Sub
    
    Function TestNum(s As String)
        Dim i As Integer
        Dim x As Integer
        Dim y As Integer
        
        For i = 1 To Len(s)
            x = Asc(Mid$(s, i, 1))
            If x <> 32 Then
                y = y + x
                Debug.Print "The value of variable I is: " & i
                Debug.Print "The value of variable S is: " & s
                Debug.Print "The value of variable X is: " & x
                Debug.Print "The value of variable Y is: " & y
            End If
        Next i
        TestNum = y  
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] error handling (works first time but not the second)
    By apenca in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-26-2014, 12:04 PM
  2. Formula works but Run Time Error 13 Type Mismatch in VBA
    By uberathlete in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-29-2013, 10:05 PM
  3. [SOLVED] run time error 91 - but code works in another spreadsheet ?
    By pmasrich in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-08-2013, 07:32 AM
  4. 1st Sub works the 2nd Sub a Run Time Error
    By Random_dave in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-11-2012, 04:35 AM
  5. Error on second run - works the first time
    By projecttoday in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-28-2011, 03:21 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1