+ Reply to Thread
Results 1 to 5 of 5

How to convert a "120" defined as a String to a "120" defined as Integer !! >_<

Hybrid View

  1. #1
    Registered User
    Join Date
    04-20-2006
    Location
    on Earth ^_^
    Posts
    10

    How to convert a "120" defined as a String to a "120" defined as Integer !! >_<

    Hey Folks !
    Here comes back the Crazy_vba that I am.. and still working on his code :p

    Thanks to the previous help, and the download of a pack called morefunc, I can get more advanced and easy coding now. (the website is http://xcell05.free.fr/)

    Anyway...Here is my **new** problem.

    I got in one cell this sentence (string):

    B16 : "Franchisor is a public company. Franchisor has 120 employee(s); 59 employee(s) in franchise department"

    I've tried in the past days, and thanks to your answers to extract the specific numbers 120 and 59... I used the Range("e14").Value = Val(Right(Range("b16").Value, 38)) and Range("d14").Value = Val(Right(Range("b16").Value, 55)).

    But, cos there's a But ! This method was not working as on some downloaded webpages, the sentence was longer or shorter...so the 38 and 55 values were not returning the info that I needed.

    I'm now using a more powerfull technique, with the function STEXTE= -included in the morefunc pack. Basically, I give it the cell in which I'm working, the number of words I'm looking for and their positions...

    [ "=STEXTE(RC[-1],i,1)" ] where RC[-1] is the cell I'm looking in; i is the ith word position in the cell; 1 is the number of words i'm looking for, here only one "word".

    So let's go back to our cell B16:
    "Franchisor is a public company. Franchisor has 120 employee(s); 59 employee(s) in franchise department"

    basically, with the STEXTE function, if I want to get the number 120 and 59, I will need to type the following formula:
    =STEXTE(B16;8;1) to get 120 out; and
    =STEXTE(B16;10;1) to get 59 out.

    alright so far? But you would tell me: Hey Crazy_Vba, You will still have the same problem if the NUMBERS 120 and 59 are not at the 8th/10th place in your sentence in the case of another company.
    I would say: You're right sir !

    That is why I've tried to code this was my macro:

    For i = 1 To 50
    ActiveCell.FormulaR1C1 = "=STEXTE(RC[-1],i,1)"
    test = ActiveCell.Value

    If VarType(test) = vbInteger Then
    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = "Integer"
    End If

    If VarType(test) = vbString Then
    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = "String"
    End If
    Next i

    My For ... Next is not "perfect" yet but it is not my problem (yet!)
    As you have understood by reading my code, I'm taking each word one by one (hence the for next) and "test" it with a If...End If procedure to know if it is a String or an Integer.

    However, by doing a step-by-step run of my macro, it does select each word one by one, display it, and check if it is a string or an integer... BUT IT IS ALWAYS DEFINED AND UNDERSTOOD AS A STRING ! Even if the "extracted word" is 120.

    So Here is my idea: to insert in my code a Convertion Phase, that will "try" to convert the extracted word into integer, and then, if it is an integer, the macro should stop!

    What I'm looking for would be something like that :

    For i = 1 To 50
    ActiveCell.FormulaR1C1 = "=STEXTE(RC[-1],i,1)"

    TRANSFORM ACTIVECELL.VALUE INTO INTEGER

    test = ActiveCell.Value

    If VarType(test) = vbInteger Then
    STOP MACRO

    If VarType(test) = vbString Then
    CONTINUE TO LOOK FOR THE OTHER WORDS IN THE CELL
    End If
    Next i

    Any idea ? :-)
    I'm dead, I've been looking thoughout the forum with key words like "convert strings integer double", even on google, but cannot find exactly what I need!

    how to convert a damn "120" defined as a string into a "120" defined as integer ! :-) and cook it with a nice For...Next procedure !

    Thanks in advance for your consideration and help guys!
    Crazy Vba

  2. #2
    Tim Williams
    Guest

    Re: How to convert a "120" defined as a String to a "120" defined as Integer !! >_<

    cint(stringVariable)

    Tim

    "crazy_vba" <[email protected]> wrote in message
    news:[email protected]...
    >
    > Hey Folks !
    > Here comes back the Crazy_vba that I am.. and still working on his code
    > :p
    >
    > Thanks to the previous help, and the download of a pack called
    > morefunc, I can get more advanced and easy coding now. (the website is
    > http://xcell05.free.fr/)
    >
    > Anyway...Here is my **new** problem.
    >
    > I got in one cell this sentence (string):
    >
    > B16 : "Franchisor is a public company. Franchisor has 120 employee(s);
    > 59 employee(s) in franchise department"
    >
    > I've tried in the past days, and thanks to your answers to extract the
    > specific numbers 120 and 59... I used the Range("e14").Value =
    > Val(Right(Range("b16").Value, 38)) and Range("d14").Value =
    > Val(Right(Range("b16").Value, 55)).
    >
    > But, cos there's a But ! This method was not working as on some
    > downloaded webpages, the sentence was longer or shorter...so the 38 and
    > 55 values were not returning the info that I needed.
    >
    > I'm now using a more powerfull technique, with the function STEXTE=
    > -included in the morefunc pack. Basically, I give it the cell in which
    > I'm working, the number of words I'm looking for and their
    > positions...
    >
    > [ "=STEXTE(RC[-1],i,1)" ] where RC[-1] is the cell I'm looking in; i is
    > the ith word position in the cell; 1 is the number of words i'm looking
    > for, here only one "word".
    >
    > So let's go back to our cell B16:
    > "Franchisor is a public company. Franchisor has 120 employee(s); 59
    > employee(s) in franchise department"
    >
    > basically, with the STEXTE function, if I want to get the number 120
    > and 59, I will need to type the following formula:
    > =STEXTE(B16;8;1) to get 120 out; and
    > =STEXTE(B16;10;1) to get 59 out.
    >
    > alright so far? But you would tell me: Hey Crazy_Vba, You will still
    > have the same problem if the NUMBERS 120 and 59 are not at the 8th/10th
    > place in your sentence in the case of another company.
    > I would say: You're right sir !
    >
    > That is why I've tried to code this was my macro:
    >
    > For i = 1 To 50
    > ActiveCell.FormulaR1C1 = "=STEXTE(RC[-1],i,1)"
    > test = ActiveCell.Value
    >
    > If VarType(test) = vbInteger Then
    > ActiveCell.Offset(0, 1).Select
    > ActiveCell.Value = "Integer"
    > End If
    >
    > If VarType(test) = vbString Then
    > ActiveCell.Offset(0, 1).Select
    > ActiveCell.Value = "String"
    > End If
    > Next i
    >
    > My For ... Next is not "perfect" yet but it is not my problem (yet!)
    > As you have understood by reading my code, I'm taking each word one by
    > one (hence the for next) and "test" it with a If...End If procedure to
    > know if it is a String or an Integer.
    >
    > However, by doing a step-by-step run of my macro, it does select each
    > word one by one, display it, and check if it is a string or an
    > integer... BUT IT IS ALWAYS DEFINED AND UNDERSTOOD AS A STRING ! Even
    > if the "extracted word" is 120.
    >
    > So Here is my idea: to insert in my code a Convertion Phase, that will
    > "try" to convert the extracted word into integer, and then, if it is an
    > integer, the macro should stop!
    >
    > What I'm looking for would be something like that :
    >
    > For i = 1 To 50
    > ActiveCell.FormulaR1C1 = "=STEXTE(RC[-1],i,1)"
    >
    > TRANSFORM ACTIVECELL.VALUE INTO INTEGER
    >
    > test = ActiveCell.Value
    >
    > If VarType(test) = vbInteger Then
    > STOP MACRO
    >
    > If VarType(test) = vbString Then
    > CONTINUE TO LOOK FOR THE OTHER WORDS IN THE CELL
    > End If
    > Next i
    >
    > Any idea ? :-)
    > I'm dead, I've been looking thoughout the forum with key words like
    > "convert strings integer double", even on google, but cannot find
    > exactly what I need!
    >
    > how to convert a damn "120" defined as a string into a "120" defined as
    > integer ! :-) and cook it with a nice For...Next procedure !
    >
    > Thanks in advance for your consideration and help guys!
    > Crazy Vba
    >
    >
    > --
    > crazy_vba
    > ------------------------------------------------------------------------
    > crazy_vba's Profile: http://www.excelforum.com/member.php...o&userid=33679
    > View this thread: http://www.excelforum.com/showthread...hreadid=539716
    >




  3. #3
    Registered User
    Join Date
    04-20-2006
    Location
    on Earth ^_^
    Posts
    10
    ain't working :-/
    In fact,
    in my For...Next procedure, there is the moment when the "string variable" IS a string... and then I got a error msg saying : Incompatibilty of Type...
    How can you use this cint function with a For...Next procedure?
    Thanks Tim !

    cint(stringVariable)

    Tim

  4. #4
    Tim Williams
    Guest

    Re: How to convert a "120" defined as a String to a "120" defined as Integer !! >_<

    Try this:

    '**********************************************
    'Returns the iNum'th whole number from sText
    'Eg: =GetNumber("10 or maybe 45 employees",2)
    Function GetNumber(sText As String, iNum As Integer) As Variant

    Static regEx As Object
    Dim m
    Dim i As Integer

    If regEx Is Nothing Then
    Set regEx = CreateObject("vbscript.regexp")
    regEx.Pattern = "(\d+)"
    regEx.Global = True
    regEx.IgnoreCase = True
    End If

    Set m = regEx.Execute(sText)
    If m.Count >= iNum Then
    GetNumber = m(iNum - 1)
    Else
    GetNumber = ""
    End If

    End Function
    '**********************************************

    Tim



    "crazy_vba" <[email protected]> wrote in message
    news:[email protected]...
    >
    > Hey Folks !
    > Here comes back the Crazy_vba that I am.. and still working on his code
    > :p
    >
    > Thanks to the previous help, and the download of a pack called
    > morefunc, I can get more advanced and easy coding now. (the website is
    > http://xcell05.free.fr/)
    >
    > Anyway...Here is my **new** problem.
    >
    > I got in one cell this sentence (string):
    >
    > B16 : "Franchisor is a public company. Franchisor has 120 employee(s);
    > 59 employee(s) in franchise department"
    >
    > I've tried in the past days, and thanks to your answers to extract the
    > specific numbers 120 and 59... I used the Range("e14").Value =
    > Val(Right(Range("b16").Value, 38)) and Range("d14").Value =
    > Val(Right(Range("b16").Value, 55)).
    >
    > But, cos there's a But ! This method was not working as on some
    > downloaded webpages, the sentence was longer or shorter...so the 38 and
    > 55 values were not returning the info that I needed.
    >
    > I'm now using a more powerfull technique, with the function STEXTE=
    > -included in the morefunc pack. Basically, I give it the cell in which
    > I'm working, the number of words I'm looking for and their
    > positions...
    >
    > [ "=STEXTE(RC[-1],i,1)" ] where RC[-1] is the cell I'm looking in; i is
    > the ith word position in the cell; 1 is the number of words i'm looking
    > for, here only one "word".
    >
    > So let's go back to our cell B16:
    > "Franchisor is a public company. Franchisor has 120 employee(s); 59
    > employee(s) in franchise department"
    >
    > basically, with the STEXTE function, if I want to get the number 120
    > and 59, I will need to type the following formula:
    > =STEXTE(B16;8;1) to get 120 out; and
    > =STEXTE(B16;10;1) to get 59 out.
    >
    > alright so far? But you would tell me: Hey Crazy_Vba, You will still
    > have the same problem if the NUMBERS 120 and 59 are not at the 8th/10th
    > place in your sentence in the case of another company.
    > I would say: You're right sir !
    >
    > That is why I've tried to code this was my macro:
    >
    > For i = 1 To 50
    > ActiveCell.FormulaR1C1 = "=STEXTE(RC[-1],i,1)"
    > test = ActiveCell.Value
    >
    > If VarType(test) = vbInteger Then
    > ActiveCell.Offset(0, 1).Select
    > ActiveCell.Value = "Integer"
    > End If
    >
    > If VarType(test) = vbString Then
    > ActiveCell.Offset(0, 1).Select
    > ActiveCell.Value = "String"
    > End If
    > Next i
    >
    > My For ... Next is not "perfect" yet but it is not my problem (yet!)
    > As you have understood by reading my code, I'm taking each word one by
    > one (hence the for next) and "test" it with a If...End If procedure to
    > know if it is a String or an Integer.
    >
    > However, by doing a step-by-step run of my macro, it does select each
    > word one by one, display it, and check if it is a string or an
    > integer... BUT IT IS ALWAYS DEFINED AND UNDERSTOOD AS A STRING ! Even
    > if the "extracted word" is 120.
    >
    > So Here is my idea: to insert in my code a Convertion Phase, that will
    > "try" to convert the extracted word into integer, and then, if it is an
    > integer, the macro should stop!
    >
    > What I'm looking for would be something like that :
    >
    > For i = 1 To 50
    > ActiveCell.FormulaR1C1 = "=STEXTE(RC[-1],i,1)"
    >
    > TRANSFORM ACTIVECELL.VALUE INTO INTEGER
    >
    > test = ActiveCell.Value
    >
    > If VarType(test) = vbInteger Then
    > STOP MACRO
    >
    > If VarType(test) = vbString Then
    > CONTINUE TO LOOK FOR THE OTHER WORDS IN THE CELL
    > End If
    > Next i
    >
    > Any idea ? :-)
    > I'm dead, I've been looking thoughout the forum with key words like
    > "convert strings integer double", even on google, but cannot find
    > exactly what I need!
    >
    > how to convert a damn "120" defined as a string into a "120" defined as
    > integer ! :-) and cook it with a nice For...Next procedure !
    >
    > Thanks in advance for your consideration and help guys!
    > Crazy Vba
    >
    >
    > --
    > crazy_vba
    > ------------------------------------------------------------------------
    > crazy_vba's Profile: http://www.excelforum.com/member.php...o&userid=33679
    > View this thread: http://www.excelforum.com/showthread...hreadid=539716
    >




  5. #5
    Registered User
    Join Date
    04-20-2006
    Location
    on Earth ^_^
    Posts
    10
    The problem here is that I don't understand what you're doing in your code :-/
    Too complex

    How does it work and Where Should I use it / put it in my macro ? (it is my first time using a function in vba :-(

    How can I implement a function in my macro with the For...Next procedure Tim?

    Quote Originally Posted by Tim Williams
    Try this:

    '**********************************************
    'Returns the iNum'th whole number from sText
    'Eg: =GetNumber("10 or maybe 45 employees",2)
    Function GetNumber(sText As String, iNum As Integer) As Variant

    Static regEx As Object
    Dim m
    Dim i As Integer

    If regEx Is Nothing Then
    Set regEx = CreateObject("vbscript.regexp")
    regEx.Pattern = "(\d+)"
    regEx.Global = True
    regEx.IgnoreCase = True
    End If

    Set m = regEx.Execute(sText)
    If m.Count >= iNum Then
    GetNumber = m(iNum - 1)
    Else
    GetNumber = ""
    End If

    End Function
    '**********************************************

    Tim



    "crazy_vba" <[email protected]> wrote in message
    news:[email protected]...
    >
    > Hey Folks !
    > Here comes back the Crazy_vba that I am.. and still working on his code
    > :p
    >
    > Thanks to the previous help, and the download of a pack called
    > morefunc, I can get more advanced and easy coding now. (the website is
    > http://xcell05.free.fr/)
    >
    > Anyway...Here is my **new** problem.
    >
    > I got in one cell this sentence (string):
    >
    > B16 : "Franchisor is a public company. Franchisor has 120 employee(s);
    > 59 employee(s) in franchise department"
    >
    > I've tried in the past days, and thanks to your answers to extract the
    > specific numbers 120 and 59... I used the Range("e14").Value =
    > Val(Right(Range("b16").Value, 38)) and Range("d14").Value =
    > Val(Right(Range("b16").Value, 55)).
    >
    > But, cos there's a But ! This method was not working as on some
    > downloaded webpages, the sentence was longer or shorter...so the 38 and
    > 55 values were not returning the info that I needed.
    >
    > I'm now using a more powerfull technique, with the function STEXTE=
    > -included in the morefunc pack. Basically, I give it the cell in which
    > I'm working, the number of words I'm looking for and their
    > positions...
    >
    > [ "=STEXTE(RC[-1],i,1)" ] where RC[-1] is the cell I'm looking in; i is
    > the ith word position in the cell; 1 is the number of words i'm looking
    > for, here only one "word".
    >
    > So let's go back to our cell B16:
    > "Franchisor is a public company. Franchisor has 120 employee(s); 59
    > employee(s) in franchise department"
    >
    > basically, with the STEXTE function, if I want to get the number 120
    > and 59, I will need to type the following formula:
    > =STEXTE(B16;8;1) to get 120 out; and
    > =STEXTE(B16;10;1) to get 59 out.
    >
    > alright so far? But you would tell me: Hey Crazy_Vba, You will still
    > have the same problem if the NUMBERS 120 and 59 are not at the 8th/10th
    > place in your sentence in the case of another company.
    > I would say: You're right sir !
    >
    > That is why I've tried to code this was my macro:
    >
    > For i = 1 To 50
    > ActiveCell.FormulaR1C1 = "=STEXTE(RC[-1],i,1)"
    > test = ActiveCell.Value
    >
    > If VarType(test) = vbInteger Then
    > ActiveCell.Offset(0, 1).Select
    > ActiveCell.Value = "Integer"
    > End If
    >
    > If VarType(test) = vbString Then
    > ActiveCell.Offset(0, 1).Select
    > ActiveCell.Value = "String"
    > End If
    > Next i
    >
    > My For ... Next is not "perfect" yet but it is not my problem (yet!)
    > As you have understood by reading my code, I'm taking each word one by
    > one (hence the for next) and "test" it with a If...End If procedure to
    > know if it is a String or an Integer.
    >
    > However, by doing a step-by-step run of my macro, it does select each
    > word one by one, display it, and check if it is a string or an
    > integer... BUT IT IS ALWAYS DEFINED AND UNDERSTOOD AS A STRING ! Even
    > if the "extracted word" is 120.
    >
    > So Here is my idea: to insert in my code a Convertion Phase, that will
    > "try" to convert the extracted word into integer, and then, if it is an
    > integer, the macro should stop!
    >
    > What I'm looking for would be something like that :
    >
    > For i = 1 To 50
    > ActiveCell.FormulaR1C1 = "=STEXTE(RC[-1],i,1)"
    >
    > TRANSFORM ACTIVECELL.VALUE INTO INTEGER
    >
    > test = ActiveCell.Value
    >
    > If VarType(test) = vbInteger Then
    > STOP MACRO
    >
    > If VarType(test) = vbString Then
    > CONTINUE TO LOOK FOR THE OTHER WORDS IN THE CELL
    > End If
    > Next i
    >
    > Any idea ? :-)
    > I'm dead, I've been looking thoughout the forum with key words like
    > "convert strings integer double", even on google, but cannot find
    > exactly what I need!
    >
    > how to convert a damn "120" defined as a string into a "120" defined as
    > integer ! :-) and cook it with a nice For...Next procedure !
    >
    > Thanks in advance for your consideration and help guys!
    > Crazy Vba
    >
    >
    > --
    > crazy_vba
    > ------------------------------------------------------------------------
    > crazy_vba's Profile: http://www.excelforum.com/member.php...o&userid=33679
    > View this thread: http://www.excelforum.com/showthread...hreadid=539716
    >
    Last edited by crazy_vba; 05-08-2006 at 03:47 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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