PDA

View Full Version : [SOLVED:] Just Keep 1 Row for each Account



justdream
04-01-2013, 05:59 AM
Dears,

I've 24 Hours Data for each Account, I need Macro to copy
only one row for each Account based on Maximum cost in Column "F"
and paste in next Excel tab

Below is Draft copy of my working Data
Thanks for your support

SamT
04-01-2013, 06:43 AM
What is the name of the "next tab," and what does it look like?

justdream
04-01-2013, 08:03 AM
What is the name of the "next tab," and what does it look like?

We could give it any name "Max cost Data"
and it should contains same Row Main header as in Draft "sheet1"
plus Macro output which is
Complete copy of Row Data for each account where its Hourly Cost reach maximum value

justdream
04-01-2013, 08:16 AM
Output should be like this

mdmackillop
04-01-2013, 09:16 AM
Sub GetData()
Dim col As New Collection
Dim AC As String, TCH As String
Dim cel As Range
Dim i, x As Single, y As Single
AC = Range(Cells(2, 4), Cells(Rows.Count, 4).End(xlUp)).Address
TCH = Range(AC).Offset(, 2).Address
On Error Resume Next
For Each cel In Range(AC)
col.Add cel, cel
Next
On Error GoTo 0
For Each i In col
y = Evaluate("=MAX(IF(" & AC & "=" & """" & i & """" & ",TCH))")
x = Evaluate("=SUMPRODUCT(--(" & AC & " = " & """" & i & """" & "),--(TCH = " & y & "),ROW(" & AC & "))")
Cells(x, 1).Resize(, 12).Copy Sheets(2).Cells(Rows.Count, 1).End(xlUp)(2)
Next
End Sub

justdream
04-01-2013, 09:54 AM
Thanks mdmackillop
it gives me Run time error "13"
Type mismatch

and highlight this row code:


y = Evaluate("=MAX(IF(" & AC & "=" & """" & i & """" & ",TCH))")

mdmackillop
04-01-2013, 09:56 AM
I just noticed that. See revised posting

SamT
04-01-2013, 10:03 AM
Dr Mack, your XL-Dennis link is dead. :(

justdream
04-01-2013, 11:22 AM
I'm afraid it's still giving same error

mdmackillop
04-01-2013, 12:41 PM
Try this version


Sub GetData()
Dim col As New Collection
Dim AC As String, TCH As String
Dim cel As Range
Dim i, x As Single, y As Single
AC = Range(Cells(2, 4), Cells(Rows.Count, 4).End(xlUp)).Address
TCH = Range(AC).Offset(, 2).Address
On Error Resume Next
For Each cel In Range(AC)
col.Add cel, cel
Next
On Error GoTo 0
For Each i In col
y = Evaluate("=MAX(IF(" & AC & "=" & """" & i & """" & "," & TCH & "))")
x = Evaluate("=SUMPRODUCT(--(" & AC & " = " & """" & i & """" & "),--(" & TCH & " = " & y & "),ROW(" & AC & "))")
Cells(x, 1).Resize(, 12).Copy Sheets(2).Cells(Rows.Count, 1).End(xlUp)(2)
Next
End Sub

justdream
04-01-2013, 12:47 PM
Thanks a lot for giving me of your time..
This time we have Run time Error "9"
Subscript out of range

for last line on code


Cells(x, 1).Resize(, 12).Copy Sheets(2).Cells(Rows.Count, 1).End(xlUp)(2)

mdmackillop
04-01-2013, 01:02 PM
Do you have 2 sheets in your workbook?

justdream
04-01-2013, 01:06 PM
No just one "sheet1"
I've attached it in my original post

Many Thanks

justdream
04-01-2013, 01:24 PM
I got it, it's working well Thanks

Just please could tell me where in your codes you have pointed
for columns D & F
so I could change in case of different Data arrangement

justdream
04-01-2013, 01:52 PM
Sorry, Dear
I've tried Macro code with large amount of Data
but it didn't give me Max. cost for each account as required

here is the Data, feel feel to test it

mdmackillop
04-01-2013, 03:21 PM
In your data, there are 58 Accounts where the Cost is 0 so no Maximum is available.

D & F are defined here


AC = Range(Cells(2, 4), Cells(Rows.Count, 4).End(xlUp)).Address
TCH = Range(AC).Offset(, 2).Address

justdream
04-02-2013, 05:59 AM
You are right but I've detected 1 issue
in case you have repeated maximum value for Cost for same account
Macro didn't work well

Please check this sample
Could we fix it to get for example first row for highest cost value even if it;s repeated

mdmackillop
04-02-2013, 01:03 PM
Option Explicit

Sub GetData()
Dim col As New Collection
Dim AC As Range, TCH As Range
Dim cel As Range, c As Range
Dim i, x As Single, y As Single
Dim FirstAddress As String
Set AC = Range(Cells(2, 4), Cells(Rows.Count, 4).End(xlUp))
Set TCH = AC.Offset(, 2)
On Error Resume Next
For Each cel In AC
col.Add cel, cel
Next
On Error GoTo 0
For Each i In col
y = Evaluate("=MAX(IF(" & AC.Address & "=" & """" & i & """" & "," & TCH.Address & "))")
With Range("F:F")
Set c = .Find(y, LookIn:=xlValues)
If Not c Is Nothing Then
FirstAddress = c.Address
Do
If c.Offset(, -2) = i Then
c.Interior.ColorIndex = 6 'for test purposes
x = c.Row
Exit Do
End If
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
End If
End With
Cells(x, 1).Resize(, 38).Copy Sheets(2).Cells(Rows.Count, 1).End(xlUp)(2)
Next
End Sub

justdream
04-02-2013, 02:28 PM
Sorry Mate,
I know I;m disturbing you a lot
New code sometimes work and sometimes give me

Run Time Error "1004":
Application Defined or object defined error

mdmackillop
04-02-2013, 03:29 PM
It seems to be a night for "rounding" errors. Remove the Long Dim options to make this line.

Dim i, x, y