Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function Question

Status
Not open for further replies.

paispatin

Technical User
Oct 21, 2003
62
A1
I want to convert from "a" to "1" or from "b" to "2" with function.

I'm new with VB, please give me an Idea about this problem.

Thank you in advance.

Code:
a1="a"
a1 = fac(a1)
MsgBox (a1) 
'if a1="a" it should be "1"

a2="b"
a2 = fac(a2)
MsgBox (a2) 
'if a2="b" it should be "2"
    

Function fac(t) As String

    If t = "a" Then
        t = "1"
    End If

    If t = "b" Then
        t = "2"
    End If

  
End Function

Of couse this code not work, but that's just my idea about function.
 
Here goes the fac function. Note that the return type of the function is String as you proposed, but you should change it to Integer which is more appropriate.
___
[tt]
Function fac(Char As String) As String
fac = Asc(UCase$(Char)) - vbKeyA + 1
End Function[/tt]
 
Thank you Hypetia, you gave me an idea.
When I try this code:

Function fac(Char As String) As String

VB give this error :
ByRef argement type mismatch

So, I change the code into :

Function fac(t) As String

and it work.
Thank you very much.
 
Try the following it should be what you are looking for!

Code:
Option Explicit

Private Sub Command1_Click()
Dim a1 As String
a1 = "as"
MsgBox fac(a1)

End Sub
Public Function fac(letter As String) As Integer

If Asc(LCase(letter)) <= 122 And Asc(LCase(letter)) >= 97 Then
    fac = Asc(LCase(letter)) - 96
  End If
    
End Function

hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top