MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 30th, 2002, 08:35 PM   #1
StevenData
New Member
 
Join Date: Mar 2002
Posts: 10
Default

Greetings,
I am trying to convert a column of lengths in the numeric format of decimal feet (i.e. 2.1, 2.2,…2.10,12.11). how can I convert this to fractional inches(i.e. 2.08, 2.17,…,2.83,2.92). A great contributor of this forum,dk has suggested using the following macro, which beautifully works for all values except those containing the fractions .10 and .11(i.e. 2.10, 2.11). could you please help me to make dk’s macro work for all valid values.

sub convertfeetandinches()
dim cl as range
for each cl in selection.cells
if isnumeric(cl.value) then
cl.value=format(int(cl.value)+(cl.value - int(cl.value))*10/12,"#,##0.00")
end if
next
end sub
Sincerely, Steve
StevenData is offline   Reply With Quote
Old Mar 30th, 2002, 10:19 PM   #2
Dave Patton
Board Regular
 
Join Date: Feb 2002
Location: Calgary, Alberta Canada
Posts: 2,065
Default

I am not a programmer and I did not see the original question. You can consider the following:

2.01 2.0833 1/12 0.083
2.02 2.1667 1/6 0.167
2.03 2.2500 1/4 0.250
2.04 2.3333 1/3 0.333
2.05 2.4167 5/12 0.417
2.06 2.5000 1/2 0.500
2.07 2.5833 7/12 0.583
2.08 2.6667 2/3 0.667
2.09 2.7500 3/4 0.750
2.1 2.8333 5/6 0.833
2.11 2.9167 11/12 0.917

revised line

cl.Value = Format(Int(cl.Value) + (cl.Value - Int(cl.Value)) * 100 / 12, "#,##0.0000")

[ This Message was edited by: Dave Patton on 2002-03-30 21:21 ]
Dave Patton is offline   Reply With Quote
Old Mar 30th, 2002, 10:40 PM   #3
Fryer Tuck
Board Regular
 
Join Date: Mar 2002
Posts: 64
Default

Your problem is that it is treating the value to the right of the decimal place as decimal format. This is thrown for a loop with 11 and 12 inches.

Try this:
cl.value=format(int(cl.value)+ if(Int(cl.value)<>cl.value,Right(cl.value,Find(".",cl.value)-1)/12,0),"#,##0.00")
Fryer Tuck is offline   Reply With Quote
Old Mar 31st, 2002, 01:58 AM   #4
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

Hello Steven,

Sorry about the error in my original post. Here is some revised code which produces the following results:-

Before After
2.1 2.08
2.2 2.17
2.3 2.25
2.4 2.33
2.5 2.42
2.6 2.50
2.7 2.58
2.8 2.67
2.9 2.75
2.10 2.83
2.11 2.92
2.12 3.00

Code:
Sub ConvertFeetAndInches()
Dim cl As Range
For Each cl In Selection.Cells
    If IsNumeric(cl.Value) Then
        cl.Value = Format(Int(cl.Value) + Right(cl.Value, Len(cl.Value) - InStr(1, cl.Value, ".")) / 12, "#,##0.00")
    End If
Next
End Sub
HTH,
D
dk is offline   Reply With Quote
Old Mar 31st, 2002, 03:55 AM   #5
nisht
Board Regular
 
Join Date: Feb 2002
Location: Ahmedabad Gujarat
Posts: 303
Default

See if this function can be useful to you..

this function was written by damon... and i got it from a questioner.

I have added few codes to make it useable for negative values.
Actually this is for converting for inches to foot.
working with inches is always a problem with us on the site.

metric is best system... still we work on inches.

Function FormatFeetInches(r1 As Range, Optional SmallestFrac As Integer = 16) As String

' DecimalIn is the number of inches to be expressed as a whole part and
'fraction thereof
' Format will display the result in string format, reducing the
'Denominator
' to the smallest possible within the constraints of bringing the number
'to the nearest
' sixteenth. The optional parameter SmallestFrac allows you to set the
'largest
' denominator to something other than sixteenths (set to 8 for eights,
'etc.). The resulting
' number will be displayed in the format 2'-7 3/8"

Dim WholePart As Integer
Dim FractPart As Single
Dim Numerator As Integer
Dim Denominator As Integer
Dim Feet As Integer
Dim flag As Boolean

DecimalIn = r1.Value
'Debug.Print R1.Address
If DecimalIn < 0 Then
DecimalIn = Abs(DecimalIn)
flag = True
End If

WholePart = Fix(DecimalIn)
If WholePart < 12 Then
Feet = 0
Else
Feet = WholePart 12
WholePart = WholePart Mod 12
End If
FractPart = DecimalIn - Fix(DecimalIn)
Denominator = SmallestFrac
Numerator = Fix(FractPart * SmallestFrac + 0.5) '0.5 Rounds to nearest
'16 th

' Reduce numerator and denominator by factors of 2
Do Until Numerator / 2 <> Numerator 2 Or Numerator < 2
Denominator = Denominator / 2
Numerator = Numerator / 2
Loop

' Format the result into a string
If Numerator <> 0 Then
FormatFeetInches = CStr(WholePart) & " " & CStr(Numerator) & "/" & CStr(Denominator) & """"
Else
FormatFeetInches = CStr(WholePart) & """"
End If

If Feet <> 0 Then
FormatFeetInches = CStr(Feet) & "'-" & FormatFeetInches
End If

If flag = False Then
FormatFeetInches = FormatFeetInches
Else
'r1.Interior.ColorIndex = 12
FormatFeetInches = "-" & FormatFeetInches
End If
End Function

I hope this can work for you.

nishith desai
http://www.pexcel.com



[ This Message was edited by: nisht on 2002-03-31 02:59 ]
nisht is offline   Reply With Quote
Old Mar 31st, 2002, 08:31 AM   #6
Dave Patton
Board Regular
 
Join Date: Feb 2002
Location: Calgary, Alberta Canada
Posts: 2,065
Default


Attention DK

I get the same result with your formula as the formula I quickly edited except I get a different result for 2.10.
Please check the formula with 2.10; both 2.01 and 2.10 give 2.08.

Dave Patton is offline   Reply With Quote
Old Mar 31st, 2002, 08:45 AM   #7
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

Dave,

I think it was established in Steven's earlier post that the cells containing the data would be formatted as text - that is the assumption I've made. If the values are going to be formatted as numbers then the code wouldn't work and I think it could be tricky to do. I guess that you'd need to check the NumberFormat property of the cell and go from there but I'll wait and see what Steven says before doing anything else.

Regards,
Dan.

dk is offline   Reply With Quote
Old Apr 1st, 2002, 07:40 AM   #8
lenze
MrExcel MVP
 
lenze's Avatar
 
Join Date: Feb 2002
Location: Helena, MT
Posts: 13,690
Default

There's a function in the Analysis Tool Pack that does this. DOLLARDE(A1,12) will convert 2.06 (2 ft 6 inches) to 2.5. DOLLARFR()will convert it back You can uscustom formatting in your cell 00 "ft".00 "in" to make display 2 ft. 06 in when you enter 2.06
lenze is offline   Reply With Quote
Old Apr 13th, 2002, 04:00 PM   #9
StevenData
New Member
 
Join Date: Mar 2002
Posts: 10
Default

Thank you again for posting thoughtful replies to my post. The format is Numeric.
Sincerely,
Steve
StevenData is offline   Reply With Quote
Old Apr 13th, 2002, 05:01 PM   #10
Yogi Anand
MrExcel MVP
 
Join Date: Mar 2002
Location: Michigan USA
Posts: 11,452
Default

Hi StevenData:
In response to an earlier posting by you on the same topic, where we had quite a lively discussion, my final post was:

2 ft&in.00 =INT(B21)+RIGHT(B21,2)/12
2 ft&in.01 2.08
2 ft&in.02 2.17
2 ft&in.03 2.25
2 ft&in.04 2.33
2 ft&in.05 2.42
2 ft&in.06 2.50
2 ft&in.07 2.58
2 ft&in.08 2.67
2 ft&in.09 2.75
2 ft&in.10 2.01
2 ft&in.11 2.92
2 ft&in.12 3.00

StevenData, please note that I have used two digit representation for inches -- otherwise you run into problems differentiating between 2.1 and 2.10


Looks like you still do not have an answer to your liking. Please post what is it that does not work for you and then let us take it from there!


_________________
Yogi Anand
Edit: Deleted inactive web site reference from hard code signature line

[ This Message was edited by: Yogi Anand on 2003-01-19 17:41 ]
Yogi Anand is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 04:00 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes