.NET General

Moderators: None (Apply to moderate this forum)
Number of threads: 797
Number of posts: 1359

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Please Help - Convert This Excel Code To VB.NET Posted by swanley007 on 15 Dec 2011 at 6:05 PM
Hello All,

I have an application I am writing. I have been searching for a solution to what I am doing. I ran across this code for Excel. It does on a Cell, what I need on a string. Can anyone show how this code would look for VB.NET 2010 and for a sting, not a cell.

Function ExtractNumber(rCell As Range, _
     Optional Take_decimal As Boolean, Optional Take_negative As Boolean) As Double

    

Dim iCount As Integer, i As Integer, iLoop As Integer

    Dim sText As String, strNeg As String, strDec As String

    Dim lNum As String

    Dim vVal, vVal2

     

     ''''''''''''''''''''''''''''''''''''''''''

     'Written by OzGrid Business Applications

     'www.ozgrid.com

     

     'Extracts a number from a cell containing text and numbers.

     ''''''''''''''''''''''''''''''''''''''''''

    sText = rCell

    If Take_decimal = True And Take_negative = True Then

        strNeg = "-" 'Negative Sign MUST be before 1st number.

        strDec = "."

    ElseIf Take_decimal = True And Take_negative = False Then

        strNeg = vbNullString

        strDec = "."

    ElseIf Take_decimal = False And Take_negative = True Then

        strNeg = "-"

        strDec = vbNullString

    End If

    iLoop = Len(sText)

        

            For iCount = iLoop To 1 Step -1

            vVal = Mid(sText, iCount, 1)

            

    

                If IsNumeric(vVal) Or vVal = strNeg Or vVal = strDec Then

                    i = i + 1

                    lNum = Mid(sText, iCount, 1) & lNum

                        If IsNumeric(lNum) Then

                            If CDbl(lNum) < 0 Then Exit For

                        Else

                          lNum = Replace(lNum, Left(lNum, 1), "", , 1)

                        End If

                End If

                 

                If i = 1 And lNum <> vbNullString Then lNum = CDbl(Mid(lNum, 1, 1))

            Next iCount

        

     

    ExtractNumber = CDbl(lNum)

     

End Function



Thank You All In Advance
Thread Tree
swanley007 Please Help - Convert This Excel Code To VB.NET on 15 Dec 2011 at 6:05 PM



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.