+ Reply to Thread
Results 1 to 6 of 6

Convert Cell Formula to Convert Text to VBA Code

  1. #1
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Convert Cell Formula to Convert Text to VBA Code

    I am about to take on a rather large excel project for our company. The first part is coming up with how to deal with our drafting department's length inputs. In this project, our users will be entering lengths in a unique format. I have to convert that unique format to a standard Foot-Inch-Fraction format.

    To try to keep the file size down and keep the complexity down, i am looking for help converting a cell formula to VBA so i can run this on a group of cells rather than repeating my code downward multiple rows on multiple sheets.

    It is either this or come up with my own font that will convert it for me but I do not know where to start with that. Anyone care to take a stab at converting this to a VB code i can run?

    A3 = 1.0108
    B3 = 1'-1 1/2"

    The code in B3 looks like this:

    Please Login or Register  to view this content.

    An explanation of the unique format we use in our drafting department:

    Basic Feet, Inch and Fraction format = 0 . 00 00 = Feet . Inch Fraction
    The numbers left of the decimal point are feet. The first two numbers to the right of the decimal point are inches and the next two are sixteenths of an inch.

    Examples:
    4’-2" = 4.0200
    15’-5 1/4" = 15.0504
    4’-11 3/8" = 4.1106
    0’-1 15/16" = 0.0115

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Convert Cell Formula to Convert Text to VBA Code

    If the formula works and you're simply looking to keep filesize down by not repeating the formula over and over, then you can use a NAMED FORMULA without VBA.

    1) Put the original value in A3.
    2) Click on B3 (important, this is a relative formula so it will always look to the cell on the left for the raw data)
    3) Press CTRL-F3 to open the Name Wizard
    4) Create a new name as:

    Name: ConvertDraftText
    Scope: Workbook
    RefersTo: =IF(A3=0,"",IF(A3="","",(TEXT(TRUNC((ROUNDDOWN(A3,0))),"0'-")&TEXT(((TRUNC(A3,2))-(ROUNDDOWN(A3,0)))*100,"0 ")&LOOKUP(MROUND((A3-(ROUNDDOWN(A3,0))-((TRUNC(A3,2))-(ROUNDDOWN(A3,0)))),0.0001),{0,0.0001,0.0002,0.0003,0.0004,0.0005,0.0006,0.0007,0.0008,0.0009,0.001,0.0011,0.0012,0.0013,0.0014,0.0015},{"","1/16""","1/8""","3/16""","1/4""","5/16""","3/8""","7/16""","1/2""","9/16""","5/8""","11/16""","3/4""","13/16""","7/8""","15/16"""}))))

    5) Now, enter this new short formula in B3 to get your result using the named formula you created:
    =ConvertDraftText


    Assuming your formula actually works, it will continue to work with the very short formula reference to the named formula you have stored.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Convert Cell Formula to Convert Text to VBA Code

    Well i learned something new today! But that will only convert a specific cell from what i can tell. I have to have the same code repeating on multiple rows and multiple sheets. My thought was, after all is entered and we need to export this to a CSV, i can run the macro to convert the columns containing the unique formats before the export.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Convert Cell Formula to Convert Text to VBA Code

    No, it will always convert the text from the cell to the left of the cell where you enter the new formula. If you enter the formula into cell M20, it will convert the text from cell L20. It's a relative formula. That's why it was important you click on B3 before you created the named formula using a relative reference to A3.

  5. #5
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Convert Cell Formula to Convert Text to VBA Code

    Interesting. I am still in the "drawing board" phase of this project but i do like this approach and it did work great, thank you. It may get a little complex for me though as i span our data across multiple worksheets and the unique formats are in different columns on different worksheets. A VB solution would be easier in my mind but this is a great start.

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Convert Cell Formula to Convert Text to VBA Code

    Hi Py_

    It might be easier to convert the number to a string in the first place by doing an Ampersand to the number to start with.
    If A1 = 3.1105 then A1&"" will be a string that has 3.1105.

    Then =LEFT(A1&"",FIND(".",A1&"")-1) will be the integer
    Inches will be =MID($A$1&"",FIND(".",$A$1&"")+1,2)
    16th will be = =MID($A$1&"",FIND(".",$A$1&"")+3,2)

    I'd do a Vlookup into your array table above to return the fraction equivalent.

    This is just another way of looking at this problem that might make the whole formula a bit smaller.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Copy text to cell and convert to numeric formula
    By JudyHNM in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-15-2014, 11:00 PM
  2. [SOLVED] Convert Cell Formula to Code - Compile Error
    By VBblindman in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-27-2012, 03:50 PM
  3. Replies: 3
    Last Post: 01-11-2012, 08:17 AM
  4. Code That will Automatically convert a cell's text to Capital...
    By Mhz in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-27-2007, 10:57 AM
  5. [SOLVED] Convert text in cell to a range name for formula
    By Sawhney in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-13-2006, 08:10 PM

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