+ Reply to Thread
Results 1 to 12 of 12

Code explanation please

  1. #1
    Forum Contributor
    Join Date
    11-18-2008
    Location
    uk
    MS-Off Ver
    Excel 2003 and Excel 2010(sometimes)
    Posts
    139

    Code explanation please

    Hi could someone explain to me exactly what the code below is doing especially the IF Statements

    Thanks in advance

    Please Login or Register  to view this content.

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Code explanation please

    the if statements are checking specific characters in B1. if the last char is '.' then the O1 is assigned a value of 1 (no clue why it uses a formula instead of a value) then the dec01 routine is run. if the second last char in B1 is a '.' then O1 equals 2 and dec02 is run and so on
    the first If statement is duplicated for some reason and the whole thing looks inefficient to me ;-)
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Code explanation please

    it checks if "." is within the first "x" characters of the right side of "B1". It's done in a queer way though. Then it proceeds to enter some values in "o1"
    If you think that my answer was helpful, please click on the "Add to this user's Reputation" button.

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Code explanation please

    All those extra Ifs can be removed. Your code can be re-written as -

    Please Login or Register  to view this content.
    Last edited by arlu1201; 10-15-2012 at 08:31 AM. Reason: Removed extra end if at the end.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  5. #5
    Forum Contributor
    Join Date
    11-18-2008
    Location
    uk
    MS-Off Ver
    Excel 2003 and Excel 2010(sometimes)
    Posts
    139

    Re: Code explanation please

    Ok thanks for your comments

    The code is suppose to check the number of decimal places of the value in "B1" then run a rountine to format other cells to that number of decimal places

    It all seems to work well for example with the folling values

    0.1, 0.01, 0.002

    But if "B1" = 1 then it doesnt

    any ideas please

  6. #6
    Forum Contributor
    Join Date
    09-05-2012
    Location
    It's more fun in the Philippines :D
    MS-Off Ver
    Excel 2007
    Posts
    209

    Re: Code explanation please

    I think you should use the function:
    INSTR(1, Range("b1"), ".")
    Last edited by Cutter; 10-15-2012 at 01:16 PM. Reason: Removed whole post quote
    Don't forget to mark your thread as [SOLVED].

  7. #7
    Forum Contributor
    Join Date
    11-18-2008
    Location
    uk
    MS-Off Ver
    Excel 2003 and Excel 2010(sometimes)
    Posts
    139

    Re: Code explanation please

    it works fine if i put in 1. instead of 1

  8. #8
    Forum Contributor
    Join Date
    11-18-2008
    Location
    uk
    MS-Off Ver
    Excel 2003 and Excel 2010(sometimes)
    Posts
    139

    Re: Code explanation please

    ok what does that code do?

    dont know the function INSTR

  9. #9
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Code explanation please

    in your else part you assign 0 to o1 but then don't do anything else

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Code explanation please

    it would be simpler to use
    Please Login or Register  to view this content.
    to put the number of decimal places in O1. I'm not sure why you need separate routines based on the different number of dp.

  11. #11
    Forum Contributor
    Join Date
    11-18-2008
    Location
    uk
    MS-Off Ver
    Excel 2003 and Excel 2010(sometimes)
    Posts
    139

    Re: Code explanation please

    Thanks very much got it sorted now

    How do i put this thread as solved?

    May try to use the INSTR function if this will run better?

    Thanks again everyone

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Code explanation please

    how to mark your thread as Solved


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1