Results 1 to 7 of 7

Thread: [RESOLVED] format excel cell for 2 decimal places

  1. #1

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Resolved [RESOLVED] format excel cell for 2 decimal places

    I want to put some value in to my excel worksheet cell.
    i want to format the cell to display only for 2 decimal places
    for example

    VB Code:
    1. dim s as long
    2.  
    3. s=1.236767
    4. .Cells(1, 4).Value = s


    so i want the value for .Cells(1, 4).Value should be 1.23

    how can i format this??i am using late binding
    thanks
    wizkid

  2. #2
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: format excel cell for 2 decimal places

    I'm not sure I'm understanding your question correctly, but if I am, then you want 2 decimal spaces in a given range right?

    In that case, just use the numberformat


    VB Code:
    1. range(yourrangehere).NumberFormat = "###,##0.00"

    if you add another 0(zero) you get 3 decimal spaces - by removing the ,(comma) you won't get a 1.000 separator.

    Hope this helps

    /Nick

  3. #3

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Re: format excel cell for 2 decimal places

    already i tried with this
    but it is not working.i am getting error like property not valid

  4. #4
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: format excel cell for 2 decimal places

    Can i see you code?

    I normally use numberformat, but I haven't experienced errors

  5. #5

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Re: format excel cell for 2 decimal places

    thanks direktoren

    i got the answer

    formatnumber is the property to format the number.

    .Cells(stylecode_colorcode, 4).Value = FormatNumber(percen, 3)

  6. #6
    New Member
    Join Date
    Sep 2019
    Posts
    1

    Re: format excel cell for 2 decimal places

    Hi Nick,

    I need one help for the excel. How to set 3 decimal place and comma (,) in particulars cell in excel using vb.net.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] format excel cell for 2 decimal places

    you should have started a new thread for this question, it may well be that nick is no longer monitoring this thread

    the basic is
    Code:
    range("b1").NumberFormat = "0.000"
    i believe that if your locale setting is to use a comma as the decimal placeholder, it will interpret this correctly, if as mine the locale setting for decimal is period i can not really test
    trying to force it display differently to the locale setting may present some problem, but you can try replacing the period with a comma if need be
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width