Results 1 to 8 of 8

Thread: VBA Function To Extract Decimal Numbers

  1. #1
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    14

    VBA Function To Extract Decimal Numbers

    Hello everyone

    I'm confused ...
    I would like to achieve a cycle optimized to achieve the two-digit decimal
    Values ​​on the sheet were up to 2 decimal places
    Type: D100 = 12345.67 becomes E100= 67
    Type: D101 = 12345.6 becomes E100= 60
    Etc..
    Here's how to get the result
    Code:
        For Each MyVar In Range("D1:D" & Cells(Rows.Count, 2).End(xlUp).Row)
            If IsNumeric(MyVar) Then
                Cells(MyVar.Row, 9) = Int((MyVar - Int(MyVar)) * 100)
            End If
        Next
    I hope there is a different solution

  2. #2
    Member littleiitin's Avatar
    Join Date
    Aug 2011
    Posts
    90
    Rep Power
    14
    Hi PcMax,

    Definitely there are other methods to achieve your requirement.
    However I believe the method you applied is the best one.
    Why you need other solution?

    Thanks
    Rahul Singh

  3. #3
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi,

    Here is a method which doesn't require loop

    Code:
    Dim x   As String
    
    With Range("d1:d" & Range("b" & Rows.Count).End(xlUp).Row)
        x = .Address
        .Offset(, 5) = Evaluate("if(isnumber(" & x & "),(" & x & "-int(" & x & "))*100," & x & ")")
    End With
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  4. #4
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    14
    Hallo

    And if I changed the formula to avoid decimal numbers with more 12.99909938 type or how I could use the assessment process that is not possible

    Code:
    Int(MyVar * 100) Mod 100

  5. #5
    Senior Member
    Join Date
    Apr 2011
    Posts
    190
    Rep Power
    14
    Try this - a lot of steps - but should work - should not matter how many decimal places
    Code:
        Asingle = 1.2345
        Astr = Str(Asingle)
        Aint = InStr(1, Astr, ".")
        If Aint > 0 then Along = Val(Right(Astr, Len(Astr) - Aint)) else Along = 0

    ohhh - just realized you want 2 characters - so use the Mid command in place of the Right command
    xl2007 - Windows 7
    xl hates the 255 number

  6. #6
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi,

    replace

    Code:
    .Offset(, 5) = Evaluate("if(isnumber(" & x & "),(" & x & "-int(" & x & "))*100," & x & ")")
    with

    Code:
    .Offset(, 5) = Evaluate("if(isnumber(" & x & "),round((" & x & "-int(" & x & ")),2)*100," & x & ")")
    HTH
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  7. #7
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    14
    Hallo

    Wonderful!

    Code:
    Sub Admin()
        Dim x   As String
        With Range("D2:E" & Range("B" & Rows.Count).End(xlUp).Row)
            x = .Address
            .Offset(, 5) = Evaluate("if(isnumber(" & x & "),round((" & x & "-int(" & x & ")),2)*100," & x & ")")
        End With
    End Sub
    This code was looking for, you can teach Evaluate method

  8. #8
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi,

    Thanks for the feedback.
    Read Evaluate Method
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

Similar Threads

  1. VBA To Extract Email Address From Text
    By dunndealpr in forum Excel Help
    Replies: 43
    Last Post: 06-05-2019, 03:56 PM
  2. Convert a (Possibly) Very Large Positive Decimal Number to Any Base (Up To 36)
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 4
    Last Post: 04-22-2013, 04:52 PM
  3. VBA Code to extract subtotals
    By Howardc in forum Excel Help
    Replies: 2
    Last Post: 12-02-2012, 01:15 PM
  4. Extract numbers from alphanumeric values
    By tushar.tarafdar in forum Excel Help
    Replies: 3
    Last Post: 09-20-2012, 10:16 PM
  5. VBA Code to Extract data
    By Howardc in forum Excel Help
    Replies: 1
    Last Post: 07-24-2012, 11:37 PM

Posting Permissions

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