Format cells to remove up to 2 trailing zeros

  • Help me with vba code to format numbers in the style of 100=1,110=11,1000=10,1800=18...thanks

    Edited once, last by Carim ().

  • Hello,


    Are you totally sure about all the examples you have provided ?


    It would seem that dividing by 100 could do the job ... but there is an apparent exception for 110 = 11 ... unless it is 1100 = 11 ...

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Carim

    Changed the title of the thread from “Please help VBA format cells to remove 1 last zero” to “Format cells to remove 1 last zero”.
  • Try the attached file. Click the button on Sheet1.


    Code assigned to the button

  • Code
    Cám ơn bạn, xin hỏi thêm chút mong bạn giúp đỡ, trường hợp 100(2)=1(2), 1800(3)=18(3) 
  • Assuming Google Translate got it right and your post reads:


    "Thank you, please ask for a little more help, case 100(2)=1(2), 1800(3)=18(3)"


    Then try the attached with code modified to

    The code will work whether there is a bracketed number (or even bracketed text) after the main number or not.

  • KjBox

    Changed the title of the thread from “Format cells to remove 1 last zero” to “Format cells to remove up to 2 trailing zeros”.
  • What was ... All Chinese to me .... is, in fact, ... Vietnamese ;)

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • LOL, yes it is Vietnamese :D

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • LOL, yes it is Vietnamese :D

    :thumbup: :thumbup: :thumbup:


    Now that you have fully decode this mystery ...

    what about =SUBSTITUTE(A1,"00","") ... or to be more precise Hàm SUBSTITUTE

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

    Edited once, last by Carim ().

  • =SUBSTITUTE(A1,"00","") would fail if the number in the brackets was 100, i.e. A1 was "100(100)"


    Also fails if A1 is "12003400(3)" since only the last 2 zeros of the unbracketed number need to be removed.

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • If there is something in brackets, then this should work:

    =SUBSTITUTE(SUBSTITUTE(A1,"0(","("),"0(","(")

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Yes, Rory, that will remove up to 2 trailing zeros from the unbracketed number and leave anything within the brackets untouched. But, as I understand it, there may not be a bracketed number, in which case no trailing zeros are removed.

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • I did say "if there is something in brackets"... ;)


    If there might not be, then perhaps:


    =IF(COUNT(FIND("(",A1)),SUBSTITUTE(SUBSTITUTE(A1,"0(","("),"0(","("),TRIM(SUBSTITUTE(SUBSTITUTE(A1&" ","0 "," "),"0 "," ")))

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • That works for all scenarios, very neat to add a trailing space before SUBSITUTE if no bracket, then remove the space again with TRIM! Much neater than what I came up with to do it using a formula which was:


    =IF(RIGHT(A1,1)=")",SUBSTITUTE(SUBSTITUTE(A1,"0(","("),"0(","("),IF(AND(RIGHT(A1,1)="0",MID(A1,LEN(A1)-1,1)="0"),LEFT(A1,LEN(A1)-2),IF(RIGHT(A1,1)="0",LEFT(A1,LEN(A1)-1),A1)))


    However, HGVIET did specify a VBA solution, maybe his actual workbook would not work if an extra column with a formula solution was added, rather than updating the values in the required column in situ.

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

    Edited once, last by KjBox ().

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!