to get last row of each day from excel sheet - excel

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

Related

Issue regarding .Find method in VBA

In this line of code here
w6 = Range(NewNewNewDataColumn & MOPNumber":" & NewNewNewDataColumn).Find(What:=w5, after:=Range(NewNewNewDataColumn & 1), searchdirection:=xlPrevious).Row
I am looking for w5 in a column which goes
Row 1 = 25
Row 2 = 24
Row 3 = 23
.
.
.
Row 26 = 0
Row 27 = 1
Row 28 = 2
.
.
.
Row 49 = 23
Row 50 = 48
Row 51 = 25
This method is used as these number are referance points for Cells that contain the useful information.
where w5 is the number 0 to 25. Works fine expect for one slightly major issue, which is instead of finding the number "1" its fine the first mention of 1 so in this case "21" So will give back the row number for the number 21 not the number 1. Is there a way to change the code to search for the entire mention of w5. Does that make sense?
Thank for any help your able to provide
Mark
w6 = Range(NewNewNewDataColumn & ":" & NewNewNewDataColumn).Find(What:=w5, after:=Range(NewNewNewDataColumn & 1), LookAt:=xlWhole, searchdirection:=xlNext).Row
Above is the line of code thatg I used and solved it, the LookAt:=xlWhole i belive was the critical statement within the line enabling the code to work as intended.

Why does Excel 2013 round up result in a cell?

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.

Cannot make .Offset to work in excel vba macro

I am stuck, and would like a little hlep. I'm trying to get a qty on Column D12, d14, d16 .... to d42. Than multiply this qty by a value. the value sheet look like this.
Value Sheet
A B C
ItemName Quality Confort
1 Chair 2 1
2 Bed 0 3
3 Table 1 1
Quantity Sheet
A B C D
ItemName QTYColumn
12 .. Table 2
13
14 .. Chair 5
15
16 .. Bed 6
Total Sheet
A B
Quality 12 (2*1 + 5*2 + 6*0 )
Confort 25 (2*1 + 5*1 + 6*3 )
I'm pretty sur I have the hardest part done. I can check and grab the quantity from all the sheets I want. I also got a function done where you pass the name of the item, and the stats name, and it return me the results I want.
so, I got this part of the code atm which doesnt work, and its driving me nuts.
For Counter = 12 To 42 Step 2
For Each qColumn In QTYColumn
Set QTY = Range(qColumn & Counter)
Dim ItemName As Range
ItemName= QTY.Offset(-2, 0).Select
total = total + (QTY * GetValue(ItemName, "Confort"))
Next qColumn
Next Counter
My problem is with the ItemName variable. Its always empty and as soon as I get to it with the debugger, the function stops and it closes. Anyone have any idea as to why ? it's important for me to get it base on the offset -2 and not the column adress because it might be different depending of the sheet, and the only "sure" way to find it is the get the 2nd cell to the left of the quantity cell.
ItemName= QTY.Offset(-2, 0).Select does not mean anything !
Either you Select:
QTY.Offset(-2, 0).Select
or you get the value:
ItemName= QTY.Offset(-2, 0).Value '(value can be omitted here)
But then, Dim ItemName As Range does not make sense. It should be a String or a a number.
or you get the range:
Set ItemName= QTY.Offset(-2, 0) ' then you need Set

How to Loop through a column in VBA with Muiltiple columns?

I am trying to highlight expiry dates in Excel VBA. I need Column B to Highlight dates that will expire within the next month and Column c to highlight dates that will expire within 4 - 6 months. At present the code works if I delete columns and run the code on one column at a time e.g.
Name Green Will Expire within 1 Month
Name 1 01/01/2013
Name 2 17/07/2013
Name 3 03/04/2013
Name 4 24/03/2013
Name 5 16/07/2013
Name 6 26/01/2013
Name 7 28/06/2013
Name 8 01/07/2013
Name 9 09/01/2013
Name 10 31/07/2013
Name (Column A), Green Will Expire within 1 Month (Column B).
If I run the code with these two columns only (Having deleted Column C) the code works fine. If I include my third column, Orange Expires in 4 - 6 Months (Column C):
Name Green Will Expire within 1 Month Orange Expires in 4 - 6 Months
Name 1 01/01/2013 01/01/2013
Name 2 17/07/2013 01/12/2013
Name 3 03/04/2013 03/04/2013
Name 4 24/03/2013 20/11/2013
Name 5 16/07/2013 16/07/2013
Name 6 26/01/2013 26/01/2013
Name 7 28/06/2013 28/06/2013
Name 8 01/07/2013 01/07/2013
Name 9 09/01/2013 09/01/2013
Name 10 31/07/2013 31/07/2013
Only the second for loop works. I need both of these for loops to run without having to delete any columns.
VBA Code:
Private Sub CommandButton1_Click()
Dim Cell As Range
For Each Cell In Range("B2:B1000").Cells
If Cell.Value <= Date + 30 And Not (Cell.Value < Date) And IsEmpty(Cell.Offset(0, 1).Value) Then
Cell.Interior.ColorIndex = 35
End If
Next Cell
For Each Cell In Range("C2:C1000").Cells
If Cell.Value <= Date + 180 And Not (Cell.Value <= Date + 120) And IsEmpty(Cell.Offset(0, 1).Value) Then
Cell.Interior.ColorIndex = 45
End If
Next Cell
End Sub
Any Help is greatly appreciated.
Thank You
The part of your conditional statement will always evaluate to False if Column C is not empty:
And IsEmpty(Cell.Offset(0, 1).Value)
On a related note, the same fragment is in the second loop, so you may want to remove it from there, too. But I am not sure if that part would be required logic. In any case, the same holds: If column D is not empty, this will always evaluate to false, so the body of the If...Then will be omitted.

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