Using Excel for Mac. I want the formula to round decimal numbers to fractions. The smallest increment I would want is 1/16 (which I can easily do) but I want automatically to reduce sixteenths to 1/8, 1/4, and 1/2 when appropriate (not rounding here).
Only rounding to the nearest sixteenth. (I have all the fractions rounding to the nearest 1/16)
But THEN reduce the fractions, eg:
14/16 - this should become 7/8
12/16 - this should become 3/4
8/16 - this should become 1/2
So in the end, I will have a large Excel sheet of fractions, some as small as 1/16, and some larger.
How would I do this?
I gather you do have some functionality for formatting fractions. Excel 2007 for example has:
I was contemplating 'doing the math' to achieve numbers (decimals) in multiples of 0.0625 and then applying formatting just for appearances.
This will work for values between 0 and 1 . Select the cells and run this small macro:
Sub FixFormat()
Dim r As Range, v As Variant, s As String
Dim numerator As Long
Selection.NumberFormat = "General"
For Each r In Selection
v = r.Value
If v >= 0 And v <= 1 Then
r.NumberFormat = "??/16"
numerator = CLng(Split(r.Text, "/")(0))
Select Case numerator
Case 0, 1, 3, 5, 7, 9, 11, 13, 15
Case 2, 6, 10, 14
r.NumberFormat = "# ?/8"
Case 4, 12
r.NumberFormat = "?/4"
Case 8
r.NumberFormat = "?/2"
End Select
End If
Next r
End Sub
The macro will detect the value and apply the proper formatting.
Rounding to 1/16ths
ROUND(YourNumber*16,0)/16
Then set the cell to two digit fractions. They will automatically reduce to 1/2, 1/4, 1/8, 1/16. Rounding drops off the "extra" portion. This could be used with round up and round down as well.
Related
I have a table of values that I am trying to pull a number from by using a for loop that chooses the right table column based a value that was input into my spreadsheet. The input value (FvX2) is rounded to the nearest tenths place using the Ceiling function. This is then compared to values in the table by increasing the column number until the rounded value in the table equals the rounded number input into the spreadsheet. My problem is excel will not recognize when the two values are equal and never returns a value for Fvcl. I think this has something to do with the ceiling function but don't know how to get around it. Any ideas? Input value is 0.52 and is rounded to 0.6. Table values start at 0.1 and increase to 0.6 by increments of 0.1. Excel will not return Fvcl when FvX2 equals table value.
Dim FvX1 As Double
Dim FvX2 As Double
Dim Fvcl As Double
FvX2 = WorksheetFunction.Ceiling(Worksheets("Sheet1").Range("B7").Value, 0.1)
FvX1 = FvX2 - 0.1
For j = 0 To 5
If Worksheets("Tables").Cells(11, 2 + j).Value = FvX2 Then
Fvcl = 2 + j
End If
Next j
How can I generate random numbers 0 or 1 in 10 cells in the row, in which the sum of the random number is always equal to 7?
enter image description here
Here's a way to get seven "1"s and three "0"s in random order using RAND and RANK
In A1:J1: =RAND()
In A2:J2: =IF(RANK(A1,$A$1:$J$1,1)>3,1,0)
Available here is a version that I really think works! https://www.dropbox.com/s/ec431fu0h0fhb5i/RandomNumbers.xlsx?dl=0
And here's the '0 and 1' version (sheet 2 at the above link):
De-dup Rankings Randoms First Cut Sorted
0.47999002 7 0.479992063 1 1
0.68823003 3 0.688233075 1 1
0.07594004 9 0.075938331 1 1
0.02077005 10 0.020766892 1 0
0.69217006 2 0.692170173 1 0
0.73355007 1 0.733549516 1 1
0.51546008 6 0.515462872 1 1
0.62308009 4 0.623078278 0
0.33033001 8 0.330331577 1
0.561260011 5 0.561260557 1
Formulae for columns A-C exactly as before, D is just 7 1's, E is:
=VLOOKUP(ROW(E2)-1,B$1:D$11,3,FALSE)
Assuming that you want a list of positive random numbers that add to 7 you can use this following method.
Enter a 0 in the top-left cell (Blue Cell).
Enter =RAND()*7 into the next 9 cells below the 0 (Orange Cells).
Enter a 7 in the cell below the 9 random values (Blue Cell).
Copy the 9 random values and paste-special-values over top to turn the formulas into values.
Sort just these 9 cells in ascending order
In the cell just to the right of the first random value put a formula that subtracts the cell to the left and one above from the cell to the left (Yellow Cells).
Repeat this formula down to the cell next to the 7 that was typed in.
Sum the values in the second column (Green Cell).
That should give you 10 random values whose sum is exactly 7.
The only issue is that getting the values to be between 0 and 1 will take a bit of trial and error.
It appears that trial and error may not be practical. It's about a one in 2,710 times that this list will contain only numbers between 0 and 1. Not overly practical. Sorry.
To answer the question in the post, enter this in A1:J1 as an array formula (ctrl+shift+enter):
=1-(TRANSPOSE(MOD(SMALL(RANDBETWEEN(0,1e12*(ROW(INDIRECT("1:10"))>0))+(ROW(INDIRECT("1:10"))-1)/10,ROW(INDIRECT("1:10"))),1))>0.65)
To answer the question in the post title, do the following:
In A1:J1 enter:
=RAND()
In K1 enter:
=IF(SUM(A1:J1)<7,(7-SUM(A1:J1))/(COUNT(A1:J1)-7),7/SUM(A1:J1))
In L1 enter:
=IF(SUM($A1:$J1)<7,(A1+$K1)/($K1+1),A1*$K1)
Fill over to U1.
I believe the 10 numbers generated will be identically distributed in [0,1), but obviously not uniformly (I'm fairly certain the distribution does not have a name). The numbers can't be considered independent. A few statistics on the distribution:
Mean: 0.7 (as expected)
The other statistics are estimated from 10,000 simulations:
Variance: 0.0295
Kurtosis: -0.648
Skewness: -0.192
Think of it as drawing a sample of size 7 from the set {1, 2, ..., 10}. The 1s correspond to the numbers chosen for inclusion in the sample. Here is some VBA code which generates such samples:
Function sample(n As Long, k As Long) As Variant
'returns a variant of length n
'consisting of k 1s and n-k 0s
'thought of as a sample of {1,...,n} of size k
Dim v As Variant 'vector to hold sample
Dim numbers As Variant
Dim i As Long, j As Long, temp As Long
ReDim v(1 To n)
ReDim numbers(1 To n)
For i = 1 To n
v(i) = 0
numbers(i) = i
Next i
'do k steps of a Fisher-Yates shuffle on numbers
For i = 1 To Application.WorksheetFunction.Min(k, n - 1)
j = Application.WorksheetFunction.RandBetween(i, n)
If i < j Then 'swap
temp = numbers(i)
numbers(i) = numbers(j)
numbers(j) = temp
End If
Next i
'now use the first k elements of the partially shuffled array:
For i = 1 To k
v(numbers(i)) = 1
Next i
sample = v
End Function
Used like: Range("A1:J1").Value = sample(10,7)
Using a bit of brute force, I think I've got a workable solution to the original version of the question which asked for random numbers between 0 and 1.
Cells A1 to A9:
=rand()
Cell A10:
=7-sum(A1:A9)
Now you have 10 numbers that add up to 7, but the last one is probably not in the range 0 to 1. To deal with that, just recalculate the sheet to generate new random numbers until that last value is within range. It takes about 25 recalculations to have a ~95% chance that one of them will be within range, so it could take a while. A little VBA can do that for you very quickly:
Sub rand7()
While Range("A10").Value > 1 Or Range("A10").Value < 0
ActiveSheet.Calculate
Wend
End Sub
I have approximately 3600 heights with the format Feet-Inch-Inch-Eigth of an inch, so for example:
5103 = 5 feet, 10 and 3/8 inches
6000 = 6 feet
6022 = 6 feet, 2 and 2/8 inches
and I need to convert these to decimal format either with a formula or macro, so that the results for these would be:
5103 = 5.864
6000 = 6.000
6022 = 6.187
As I have 3600+ of these, manual work would be a pain. How can I convert this format to a decimal format of feet and inches?
With data in column A, in B1 enter:
=--LEFT(A1,1)+MID(A1,2,2)/12+RIGHT(A1,1)/(12*8)
and copy down:
NOTE:
this conversion yields decimal feet so that 2 feet 6 inches shows 2.5
EDIT#1:
If you need to accommodate more than 9 feet in column A, then the formula should be:
=--LEFT(A1,LEN(A1)-3)+MID(A1,LEN(A1)-2,2)/12+RIGHT(A1,1)/(12*8)
If you ever need to go the long route, a VBA UDF would seem to be the most expedient.
Function feet2dec(str As String) As Double
Dim v As Long, vtmp As Variant, tmp As String
Dim feet As Double
vtmp = Split(str, Chr(44))
For v = LBound(vtmp) To UBound(vtmp)
Select Case LCase(Right(Trim(vtmp(v)), 4))
Case "feet"
feet = feet + Val(Trim(Replace(vtmp(v), "feet", vbNullString, 1, -1, vbTextCompare)))
Case "ches"
str = Replace(vtmp(v), "inches", vbNullString, 1, -1, vbTextCompare)
str = Application.Trim(Replace(str, "and", vbNullString, 1, -1, vbTextCompare))
feet = feet + Application.Evaluate(str) / 12
End Select
Next v
feet2dec = feet
End Function
I have a table with "ordered amount", "percentages", and "total sum". Note that "ordered amount" and "total sum" should be the same.
column F= sum of C:F
*rows 4,6,8,10 used =round()
As shown in the table(image), for some numbers, value in F is not the same as A when it should be. (e.g. A6(105) and F7(104), -1 after rounding).
Is there anyway to avoid this?
Thank you very much.
If the sum of the decimal points adds up to > 5, your final rounded number will round up. If they add up to < 5, the final number will round down.
Consider this example:
10.5 + 5 = 15.5, which rounds to 16.
However
10 + 5 = 15
This is what is happening in your table.
In order to get the sum of rounded numbers to add up to the same as the sum of the numbers, in your example, you will need to NOT round, but rather subtract, one of the values. I would suggest altering the largest value, as it would seem to have the least effect on the percentages, but that is a choice you can make.
To do that, with your data in row 8, for example you could do the following.
A9: =IF(B$8=MAX($B$8:$E$8),SUM($B$8:$E$8)-SUM(ROUND($B$8:$E$8,0))+ROUND(B$8,0),ROUND(B$8,0))
entered as an array formula with ctrl+shift+enter and fill right to E9.
This would give a sum of 344 which is the same as F8
I have a spreadsheet in which for each column/category, I tally up the the values for the last X days from another sheet.
This X is different for different columns/categories.
Here is the equation that I am currently suing, which tallies up to a week's worth of values, depending on the value of D$2 (the number of days that I want to tally). (Equation has been formatted to make it easier for humans to read):
= Daily!D15
+ IF(D$2 >= 2, Daily!D14, 0)
+ IF(D$2 >= 3, Daily!D13, 0)
+ IF(D$2 >= 4, Daily!D12, 0)
+ IF(D$2 >= 5, Daily!D11, 0)
+ IF(D$2 >= 6, Daily!D10, 0)
+ IF(D$2 >= 7, Daily!D9 , 0)
This works fine, as long as I only want to tally up anywhere from 1-7 days. But I would like to upgrade this spreadsheet so that it can tally an arbitrary number of of days without having to modify the equation. I'd like to be able to be able to tally, for example, 100 days without
having to create an equation with 99 IF statements in it.
Offset is really handy. It returns a range of cells relative to a given reference. In your example you'd use:
=SUM(OFFSET(Daily!D15,0,0,-D$2,1))
To explain: Starting with the reference Daily!D15, we shift 0 rows and 0 columns, then select a range with D$2 rows and 1 column. Since -D$2 is negative, the range is expanded upward, rather than downward.
The last two arguments ('height' and 'width') are optional -- if left out, the range returned will have the same dimensions as the range we provided. In fact, in this case we could have used =SUM(OFFSET(Daily!D15,0,0,-D$2)), and the width of 1 column would have been implied by the width of Daily!D15. For readability, though, I like to include both dimensions or neither.