Consulting

Results 1 to 5 of 5

Thread: Code help

  1. #1

    Code help

    Can someone tell me why my code will not let me type 1.0, 2.0, 3.0, etc, but it will allow me to type 1.1, 2.1, 3.1, etc? If possible offer a solution to my delima. Thanks in advance.

    Private Sub txtTOTAL_Change()
    Dim v As String, tVal As Single
    If Len(Me.txtTOTAL) Then
    v = Replace(Me.txtTOTAL, ".", "")
    tVal = Val(v) / 10
    Me.txtTOTAL = tVal
    End If
    End Sub

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    If you start with 1.0, Replace returns the string "10". Then tVal returns the Single 1, as you divided 10 by 10.

    If you start with 1.2, Replace returns the string "12". Then tVal return 1.2, as you divided 12 by 10.

    Does that help?

    Mark

  3. #3
    Thanks for the explaination GTO. The problem I'm having is it will not allow me to enter 1.0. It allows me to enter 1, but i will not allow me to enter 0.

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    Private Sub txtTotal_AfterUpdate()
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub txtTOTAL_Change()
    Dim v As String, tVal As Single
    Dim ReEntry As Boolean

    If Not ReEntry Then

    ReEntry = True

    With Me.txtTOTAL

    If Len(.Text) Then
    v = Replace(.Text, ".", "")
    tVal = Val(v) / 10
    .Text = tVal
    End If
    End With

    ReEntry = False
    End If
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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