Why does Excel 2013 round up result in a cell? - excel

I Create a new Excel 2013 file
I select the three first cells in column A
I select format cell
I choose Number
I check Use thousand separator
I choose 0 decimal
I enter in A1 the number 961748947
I enter in A2 the number 961748941
I enter in A3 = A1 * A2
Excel displays in A3 the value of 924 961 031 285 115 000
instead of 924 961 031 285 115 127
Why does Excel 2013 round up the value ?
How to get the exact result ?

As #tigeravatar said, Excel only has precision up to 15 digits. But from this post #10, you can use a custom function:
Function Times(ParamArray v())
Dim j As Long
Times = CDec(1)
For j = 0 To UBound(v)
Times = Times * v(j)
Next j
If WorksheetFunction.Log10(Times) > 15 Then
Times = FormatNumber(Times, , , vbTrue)
End If
End Function
This way you can have the correct outcome.

Related

excel concatenate with "²" or squared symbol

I would like to use a concatenate formula in Excel that also includes the superscript number 2 (squared symbol), but this does not work with the following input. The formula is roughly like this:
=CONCATENATE("13";"²")
i want to get an output like this:
13²
I tried using superscript functions on the font
but it didn't work when the text is inside the concatenation.
I have to use concatenate because I want to automate.
Is it possible to do with Excel 2019 there? or is it possible but would require VBA?
Try
=CONCATENATE("13";CHAR(178))
Using superscript digits via udf
Superscript digits ranging from 0 to 9 have code values in different unicode sections. Thus using the following user defined function (udf) allows you to get each digit from 0 to 9 as superscript character without the need to detect & memorize each code value again and again.
The hexadecimal and decimal codes are commented in the function below:
Function sup(ByVal digit As Long)
Dim n As Long
Select Case digit
Case 0: n = &H2070 ' dec 8304
Case 1: n = &HB9 ' dec 185
Case 2: n = &HB2 ' dec 178
Case 3: n = &HB3 ' dec 179
Case Else ' <super> 4 .. 9
n = &H2074 + digit - 4 ' dec 8308 .. 8313
End Select
sup = ChrW(n)
End Function
Some examples (~~> output x³)
="x" & sup(3)
=CONCAT("x",sup(3))
=CONCAT("x",sup(Sheet1!A2)) ' assuming a digit value of 3 in Sheet1!A2
or within VBA assignments like e.g.
Sub ExampleCall()
Dim i As Long
For i = 0 To 9
Sheet1.Range("A2").Offset(i).Value = "x" & sup(i)
Next
End Sub
I also found a way to make this work without using concatenate, it just requires making a table to map to superscript numbers.
Step 1: Characters Table
Somewhere else, build a simple table with a map from "Digit" numbers to "Superscript" numbers.
In the first column, "Digit", just type something like:
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
In the second column, "Superscript", go to Excel Ribbon Insert > Symbol. In the tab "Symbol", change the comboboxes "Font" to "(normal text)" and "Subset" to "Superscripts and Subscripts". Insert now each character in its corresponding cell be sure to use "Superscript Minus" for negative numbers. Superscripts 1, 2 and 3 are located in the subset "Latin-1 Supplement".
-9 ⁻⁹
-8 ⁻⁸
-7 ⁻⁷
-6 ⁻⁶
-5 ⁻⁵
-4 ⁻⁴
-3 ⁻³
-2 ⁻²
-1 ⁻¹
0 ⁰
1 ¹
2 ²
3 ³
4 ⁴
5 ⁵
6 ⁶
7 ⁷
8 ⁸
9 ⁹
And so on. Please notice that these are not "formatted" numbers, but special symbols with no numeric value.
Step 2: Vlook Function
Put the digit you want displayed normally in a column, set up like it is in 'A1' below, i.e.:
Cell A1: 13
Then, put the number you want to be displayed in superscript format, i.e.:
Cell B1: 3
Then, use this function to create the final output.
Cell C1: =A1&VLOOKUP(B1,Sheet2!$A$1:$B$20,2,FALSE)

VBA macro to subtract one variable number (dynamic) from a fixed number

I am after a dynamic macro that subtracts numbers in C2 onwards from B2 onwards and puts them in D2 onwards.
Numbers in C2 onwards are fixed, although the user will put numbers in B2 onwards. The length of Column C (Hours Charged) changes dependent on the sheet that has been imported. The code is as follows:
Dim O As Long
O = Cells(Rows.Count, "C").End(xlUp).Row
Cells(O + 1, "C").Formula = "=SUM(C2:C" & O & ")"
Cells(O + 1, "C").Interior.Color = RGB(208, 247, 197)
Cells(O + 1, "C").Font.Bold = True
The sheet looks something like this (the first column is B, the last is D):
Hours Budgeted | Hours Charged | Variance
0 | 85 | 0
0 | 31 | 0
0 | 20 | 0
0 | 100 | 0
0 | 75 | 0
To summarise, when a user inputs a value in the Hours Budgeted (B) column, i want it to automatically subtract from the Hours Charged (C) column and display in the Variance (D) column.
The reason why this can't just be a '=C2-B2' is because I have a macro inputting all this data, as well as a separate macro that 'Resets' the sheet (removing all data and formulas).
Thanks all,
Jake.
The following code should do the trick, just add it after the code you posted.
For i = 2 To O
Cells(i, "D").FormulaR1C1 = "=RC[-1]-RC[-2]"
Next i
FormulaR1C1 lets you insert cells references that are relative to the cell you're putting the formula into, so R[2]C[3] would insert a reference to a cell three rows down and three columns to the right.

to get last row of each day from excel sheet

i need to find the last record of each day in excel if there are multiple entries
*******intention is to get EOD balance ROW OF EACH DATE********
like i have data in excel something like this
date CR_DR amount EOD balance
----------------------
7/9/2017 19:09 CR 10 10
7/10/2017 18:37 CR 25 35
7/10/2017 21:06 DR 10 25
7/11/2017 19:21 CR 15 40
7/15/2017 14:17 DR 20 20
7/17/2017 17:12 CR 100 120
7/18/2017 7:44 DR 30 90
7/18/2017 14:08 DR 50 40
7/18/2017 16:52 CR 120 160
for which i need to get data like (get the last row of each day)
7/9/2017 19:09 CR 10 10
7/10/2017 21:06 DR 10 25
7/11/2017 19:21 CR 15 40
7/15/2017 14:17 DR 20 20
7/17/2017 17:12 CR 100 120
7/18/2017 16:52 CR 120 160
Solution 1
Enter the following formula in Cell F2
=IFERROR(MAX(IF(INT($A$2:$A$10)=INT(INDEX($A$2:$A$10, MATCH(0, FREQUENCY(IF(EXACT(INT($A$2:$A$10), TRANSPOSE(INT($F$1:F1))), MATCH(ROW($A$2:$A$10), ROW($A$2:$A$10)), ""), MATCH(ROW($A$2:$A$10), ROW($A$2:$A$10))), 0))),$A$2:$A$10,0)),"")
This is an array formula so commit it by pressing Ctrl+Shift+Enter. Drag/Copy down as required.
Then in Cell G2 enter
=VLOOKUP($F2,$A$2:$D$10,COLUMN(C1)-COLUMN($A$1),FALSE)
Drag this formula across (to right) till Cell I2 and down as required.
See image for reference.
Solution 2
Instead of using an ugly looking long formula, here we'll use a helper column.
In Cell F2 enter the following formula
=MAX(IF(INT($A$2:$A$10)=INT(A2),$A$2:$A$10,0))
This is an array formula so commit it by pressing Ctrl+Shift+Enter. Drag/Copy down as required.
Then in Cell G2 enter
=IFERROR(INDEX($F$2:$F$10,MATCH(0,INDEX(COUNTIF($G$1:G1,$F$2:$F$10),0,0),0)),"")
Drag/Copy down as required.
Finally, in Cell H2 enter
=VLOOKUP($G2,$A$2:$D$10,COLUMN(C1)-COLUMN($A$1),FALSE)
Drag this formula across (to right) till Cell J2 and down as required.
See image for reference.
EDIT : As per comment.
Instead of
=VLOOKUP($G2,$A$2:$D$10,COLUMN(C1)-COLUMN($A$1),FALSE)
use below formula in Cell H2
=INDEX(B$2:B$10,MAX(IF($A$2:$A$10=$G2,ROW($A$2:$A$10)-ROW(INDEX($A$2:$A$10,1,1))+1)))
This is an array formula so commit it by pressing Ctrl+Shift+Enter. Drag this formula across (to right) till Cell J2 and down as required. Drag/Copy down as required.
Screen-Shot :
Ok, this is crude and I'm sure there are "proper" ways to do this, but it works for what you want to do. I used columns A thru D for the data. In column F I used the formula =Left(A3,5) and copied it down. You will have to modify according to your situation. Hope this helps or gets you headed in right direction. Good Luck
Sub FindLast()
'FIND LAST LISTED DATE AND COPY
Dim myCount, myCount2, myRange, myRow, x, y, r
r = 3
myCount = Sheets("Sheet1").UsedRange.Rows.Count - 2
MsgBox "Rows used are " & myCount
For x = 3 To myCount Step 1
y = Range("F" & x).Value
MsgBox "Value of x is " & x
MsgBox "Value of y is " & y
myCount2 = WorksheetFunction.CountIf(Range("F3:F" & myCount + 2), y)
MsgBox "myCount2 value is " & myCount2
Max_date = Application.WorksheetFunction.Max(Range("A" & x & ":" & "A" & x + myCount2 - 1))
myRow = Range("A" & x + myCount2 - 1).Row
MsgBox "myRow number is: " & myRow
MsgBox CDate(Max_date)
Sheets("Sheet1").Range("I" & r).Value = Max_date
Sheets("Sheet1").Range("J" & r).Value = Range("B" & myRow).Value
Sheets("Sheet1").Range("K" & r).Value = Range("C" & myRow).Value
Sheets("Sheet1").Range("L" & r).Value = Range("D" & myRow).Value
r = r + 1
If myCount2 <> 1 Then
x = x + (myCount2 - 1)
Else
x = x
End If
Next x
End Sub
You will notice there are a lot of message boxes. I used these to test the parameters and make sure they were coming out like I wanted. When you are comfortable with how it's working, just comment them out.
Also, the output is set to I3 thru L8 for the example you posted. Please make sure to edit these if your real sheet has any data in these areas. My suggestion is test first to get familiar with what's happening. I did't put any "failsafes" or "exits" in, as I ran out of time. There are plenty of examples on this site to help with that.
Regards
It requires entering the dates in column A (I didn't feel motivated enough to figure out how to pull the unique ones out), but if you are feeling spunky, you could use some array formulas. :-)
Column A Column B C D E
7/9/2017 7/9/2017 19:09 CR 10 10
7/10/2017 7/10/2017 21:06 DR 10 25
7/11/2017 7/11/2017 19:21 CR 15 40
7/15/2017 7/15/2017 14:17 DR 20 20
7/17/2017 7/17/2017 17:12 CR 100 120
7/18/2017 7/18/2017 16:52 CR 120 160
Enter dates in column A. Then in column B enter:
=IFERROR(INDEX(Date,SMALL(IF(ROUNDDOWN(Date,0)=$A1,ROW(Date)-1),COUNTIFS(Date,">="&$A1,Date,"<"&$A1+1))),"")
closing out of the cell with CTRL-SHIFT-ENTER so that the formula in the formula bar looks like
{=IFERROR(INDEX(Date,SMALL(IF(ROUNDDOWN(Date,0)=$A1,ROW(Date)-1),COUNTIFS(Date,">="&$A1,Date,"<"&$A1+1))),"")}
Note: it does not work to enter the curly braces yourself. ;-) You must close out of the cell with CTRL-SHIFT-ENTER!
The Date in the formula is a named range - I don't like typing $A$2:$A$10 all the time. If you don't want to do named ranges, replace Date with $A$2:$A$10 or your applicable range.
Column C:
=IFERROR(INDEX(CR_DR,SMALL(IF(ROUNDDOWN(Date,0)=$A1,ROW(Date)-1),COUNTIFS(Date,">="&$A1,Date,"<"&$A1+1))),"")
Same thing - close out of cell with CTRL-SHIFT-ENTER. Named range again for CR_DR, same rules as for Date above. Copy/paste the for the other columns, changing the range that INDEX is searching as appropriate for the column you want.
Explanation: ROUNDDOWN removes the time from the date in your source data so that it can match it to the date in column A. If the date matches, its row (-1 because the row number of A2 is 2, but it is the first item in $A$2:$a$10) is put into an array for the SMALL function. This chooses the nth smallest item from the array, defined by the COUNTIFS function, which counts how many dates fall between the date in column A and the day after the date in column A (thus actually giving you the largest item). INDEX then uses the range and looks up the row number that SMALL delivered to it. If it errors out, the cell is blank.
If you don't want to hand-enter specific dates, auto-fill for all dates and it will simply be blank on dates with nothing:
7/9/2017 7/9/2017 19:09 CR 10 10
7/10/2017 7/10/2017 21:06 DR 10 25
7/11/2017 7/11/2017 19:21 CR 15 40
7/12/2017
7/13/2017
7/14/2017
7/15/2017 7/15/2017 14:17 DR 20 20
7/16/2017
7/17/2017 7/17/2017 17:12 CR 100 120
7/18/2017 7/18/2017 16:52 CR 120 160

Excel: Sum columns and rows if criteria is met

I have a sheet with product names in column I and then dates from there on. For each date there are numbers of how many pieces of a certain product have to be made. I'm trying to sum all those numbers based on a product type, i.e.:
I K L M ...
30.8. 31.8. 1.9. ...
MAD23 2 0 45 ...
MMR32 5 7 33 ...
MAD17 17 56 0 ...
MAD: 120 (2+0+45+17+56+0)
MMR: 45 (5+7+33)
What I'm doing now is sum the row first:
=SUM(K6:GN6)
MAD23 = 47
MMR32 = 45
MAD32 = 73
And then sum those numbers in column J based on part of the product name in column I:
=SUMIF(Sheet1!I6:I775;"MAD*";Sheet1!J6:J775)
MAD = 120
MMR = 45
Is it possible to do this with just one formula per criteria?
Just trying it on those three rows, I get
=SUM($K$6:$M$8*(LEFT($I$6:$I$8,LEN(I10)-1)=LEFT(I10,LEN(I10)-1)))
which is an array formula and must be entered with CtrlShiftEnter
That's assuming that I10 is going to contain some characters followed by a colon and you want to match those with the first characters of I6:I8.
=SUM(IF(MID(Sheet1!I6:I775,1,3)="MAD",Sheet1!k6:gn775,""))
With ctrl +shift+enter

Run a simulation several times with different parameters and store values

Consider a spreadsheet which performs some computations based on a fixed value (in the rxample below, D3) and is iterative. E.g.
D3: 4
B3: 12
B4: 58 (=B3*$D$3+10)
B5: 242 (=B4*$D$3+10)
B6: 978 (=B5*$D$3+10)
Total = 1290 (=sum(B2:B5))
Now, suppose I wanted to try out different values of D3 (let's call this P) and store the different totals I get, i.e.
P Total
4 1290
5 2252
6 3618
7 5460
How would I do this with Excel? A macro? Please note that the above example is a simplified version of the real thing. It should be clear that I need to compute B3-B6 so I can compute the sum.
Update:
Each computation requires several columns. E.g. we would use values on B3,B4, .. and C3,C4, ... .
A macro can do this. If B7 contains the sum formula, try this
Sub RunSimulation()
Dim p as long
for p = 4 to 7
Range("D3")=p
Debug.Print Range("B7")
Range("L" & (p-1)) = Range("B7").Value
next
End Sub
EDIT: added a line for storing the results, as requested.
If you don't want to enter the sum formula in your sheet, you can calculate the total in VBA either:
Dim total as Long
Dim row as long
total = 0
for row = 2 to 5
total = total + Range("B" & row)
Next
Debug.Print total
(Use Double instead of Long for total if you are dealing with floating point numbers.)
Usually it is done in following manner:
A B C D
1 x tmp1 tmp2 Total
2 3 $A2+10 $B2*10+10 $C2*$B2 The formulae are just for example.
3 $A2+1 $A3+10 $B3*10+10 $C3*$B3
4 $A3+1 $A4+10 $B4*10+10 $C4*$B4
Excel has capabilities to automatically increment indices in formulae.

Resources