VBA - If number is decimal then...

Solved
zivodul8 Posted messages 248 Status Member -  
blux Posted messages 5051 Registration date   Status Moderator Last intervention   -
Bonjour,

My problem is simple: I am looking for a piece of VBA code that determines whether a number is a decimal or not. For example:

If n = 3.22 then align right Else ( n = 3) align left End if 


Thank you!

7 answers

  1. beber005
     
    Hello,

    What I advise you to do is

    IF your string contains a special character here a point THEN
    align to the right
    ELSE
    align to the left
    END If

    In fact, whenever you encounter the element point in your string, you can deduce that it is a decimal number.
    0
  2. zivodul8 Posted messages 248 Status Member 22
     
    I couldn't manage to do a:

    Application.WorksheetFunction.Search


    Are there any other methods?
    0
  3. blux Posted messages 5051 Registration date   Status Moderator Last intervention   3 455
     
    Hello,

    you can do this with the int(number) function which returns the integer part of a number.

    If it is equal to the number itself, it means there is no decimal.

    IF INT(number) = number THEN ... END IF


    --
    A+ Blux
     "Fools dare to do anything. That's how we recognize them."
    0
  4. zivodul8 Posted messages 248 Status Member 22
     
    Arff, it's so silly! I'll try thanks ;-)
    0
    1. blux Posted messages 5051 Registration date   Status Moderator Last intervention   3 455
       
      Use FIX(number) instead; this function does not round.
      It will save you from testing if 3 = 2 in case you have 2.999999...
      0
    2. zivodul8 Posted messages 248 Status Member 22
       
      Thank you Blux, I almost fell for it!
      0
  5. zivodul8 Posted messages 248 Status Member 22
     
    Voici la traduction de votre message : I have another (quick) problem. I am trying to determine the line number where, in a column, two empty cells are consecutively vertical. Here's what I've done but it doesn't work:
     Sub NbLigne() Dim NbLigne, i As Integer i = 1 Do While Range("A" & i).Value <> "" And Range("A" & i + 1).Value <> "" NbLigne = i i = i + 1 Loop MsgBox NbLigne End Sub


    Thank you!
    0
  6. zivodul8 Posted messages 248 Status Member 22
     
    I have solved it; I just found out that in VBA, AND is translated as
    or
    and not
    and
    !
    0
    1. Ctesias Posted messages 786 Status Member 36
       
      No, that's wrong.
      0
    2. zivodul8 Posted messages 248 Status Member 22
       
      Hmm, to ponder...

      Thank you once again, I will not make the mistake again :)
      0
  7. Ctesias Posted messages 786 Status Member 36
     
    What you can do is:

    Dim nombre1 as integer Dim nombre2 as string Nombre1=Int(nombre2) If nombre2%nombre1 = 0 then shift left Else shift right end if
    0
    1. zivodul8 Posted messages 248 Status Member 22
       
      Also ;-)
      0
    2. blux Posted messages 5051 Registration date   Status Moderator Last intervention   3 455
       
      I'm not convinced that we can divide a string by a number...
      0
    3. Ctesias Posted messages 786 Status Member 36
       
      Hum... I quickly put a string like that. But I don't remember if there's a way to initialize numbers with decimal numbers.
      0
    4. blux Posted messages 5051 Registration date   Status Moderator Last intervention   3 455
       
      a = 9999.22
      0