____________________________________________
Please ensure you mark your thread as Solved once it is. Click here to see how
If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post
Re: Macro to extract data based on date range and values
Thanks for amending the code Robert
The values that are being extracted is more that the total value stipulated in the criteria for the date range as well as for the criteria type.
The criteria will change regularly. It would be better for the VBA code to refer to the cell/s pertaining to the criteria as opposed to it to be hard coded
It would be appreciated if you could kindly amend the code
Re: Macro to extract data based on date range and values
Howard...May I ask how long solver takes to extract the values that meet the target value...
Good Luck
I don't presume to know what I am doing, however, just like you, I too started somewhere...
One-day, One-problem at a time!!!
If you feel I have helped, please click on the star to left of post [Add Reputation]
Also....add a comment if you like!!!!
And remember...Mark Thread as Solved.
Excel Forum Rocks!!!
Re: Macro to extract data based on date range and values
I am trying to find other solution other than solver or literally looping each possible value multiple times to equate to required Target...
Image of solution posted above took my code 55 seconds...Crazy...
Re: Macro to extract data based on date range and values
I maybe have a faster solution but it requires a smart enough sheet with real dates, not poor "string date" like in the attachment
in order I can use an advanced filter, then for the sum it may be easy but depending on a crystal clear explanation of the need
as they are variations on how to calculate the sum like sequentially from a date sorted extraction,
find the better combination in order to be the closer to the sum, find the first combination equals to the sum, …
As guessing can't be coding !
Re: Macro to extract data based on date range and values
I don't know if really post #8 is what is expected 'cause of the calculation variations,
the reason why the initial post must have an explanation & an attachment, both smart enough with nothin' to guess …
What should be the result when the exact sum can't be found ? What if …
Coding before any complete context is often just a waste of time !
Re: Macro to extract data based on date range and values
Howie,
if it's an extraction for an exact reconciliation - meaning if no combination matches the sum it returns nothing -
I already have an 'instant' solution but I just expect your confirmation before writing any codeline …
According to your last attachment, on the sheet 'Extract' :
columns S & W must be empty or the advanced filter can't work as expected.
Dates & sum to reconciliate must be in cells P2:R2, dates in columns U:V are managed by the procedure.
R2 sum is optional.
This demonstration is not optimized for negative numbers,
the more data extracted, the longer reconciliation time …
You must paste these VBA procedures to the Sheet4 (Extract) worksheet module :
PHP Code:
Sub VSum(V(), S$, ByVal C@, ByVal F&) Dim R&, Z@ For R = F To UBound(V) Z = C - V(R, 1) If Z = 0 Then S = "H" & R: Exit For ElseIf Z > 0 Then VSum V, S, Z, R + 1: If S > "" Then S = S & ",H" & R: Exit For End If Next End Sub
Sub Demo1() Const F = "yyyy/m/d" Dim R, V(), S$ R = [R2].Value2: If Not IsNumeric(R) Then Beep: Exit Sub Me.UsedRange.Offset(1).Columns("A:G").Clear [T1].CurrentRegion.Offset(1).Columns("B:C").ClearContents With [T1].CurrentRegion.Rows If .Count > 1 Then .Item("2:" & .Count).Columns("B:C") = Array([P1] & Format([P2], F), [Q1] & Format([Q2], F)) Application.ScreenUpdating = False Sheet5.[A1].CurrentRegion.AdvancedFilter xlFilterCopy, .Cells, [A1].CurrentRegion End If End With With [A1].CurrentRegion.Columns("A:H") If .Rows.Count > 1 And R Then .Sort [G1], xlDescending, Header:=xlYes V = .Item(7).Value2 VSum V, S, R, 2 If S = "" Then R = 2: Beep Else Range(S) = 1 R = Application.Count(.Item(8)) + 2 .Sort [H1], , [F1], , xlAscending, , , xlYes End If If R <= .Rows.Count Then .Rows(R & ":" & .Rows.Count).Clear .Item(8).Clear Else R = .Rows.Count + 1 End If If R > 2 Then Cells(R + 1, 6).Resize(, 2) = Array(" Total :", Application.Sum(.Item(7))) End With Application.ScreenUpdating = True End Sub
► Do you like it ? ► ► So thanks to click on bottom left star icon « ★ Add Reputation » ! ◄ ◄
Re: Macro to extract data based on date range and values
Originally Posted by sintek
Image of solution posted above took my code 55 seconds...Crazy...
According to Howard' sample, my post #20 demonstration needs less than 0.1 second and
with the full data set in the sheet 'Imported Data' it requires less than 14 seconds on an old slow tests laptop …
Re: Macro to extract data based on date range and values
Thanks for your help Robert
When running your code on the workbook I used in Post # 7, I get a run time error "Application defined or object defined error and the code below is highlighted
Re: Macro to extract data based on date range and values
Howard,
I hope you have seen my post #26 for my post #20 code amended
as it's the fastest (7 to 12 times according to your initial sample) using an advanced filter & a light 'combinatorics engine' …
Here we are like in your previous thread with the near same subject where we started with a dumb data sheet
(but well ended warming a couple of neurons with a smarter sheet so needing a light Excel basics VBA code for an instant result)
but according to your initial sample, the dates & types criterias extract only ten rows, so any combinatoric way seems fast enough …
So, if you are always in this schema with few rows matching the dates & types criterias,
choose the way you better understand in case you have to maintain it.
But the more rows matching the criterias, the longer reconcilation time …
According to your attachment, I ran the worst case, dates for all the year and all types so the full data set,
my light procedure needs less than 14 seconds to display the expected result on an old slow tests laptop,
after 3 minutes without any result I stopped the execution of Sintek's code,
Trebor's result was fast but with a 'not found' message …
But maybe I was wrong with my criterias for a well setup of their procedures, if they have time to test the same …
Re: Macro to extract data based on date range and values
@ Marc L
after 3 minutes without any result I stopped the execution of Sintek's code
Geez, really...that sucks...Worked so hard on that...Did not test with a larger data set or multiple numerous criteria...Only initial Thread requirement...
Re: Macro to extract data based on date range and values
As I wrote, according to the initial schema, any of our ways well does the job,
as an user don't see the difference between my process needing less than 0.05s and yours with more than 0.3s
as this user is obviously so happy to not have to do it manually !
Like I often say, the best procedure is not always the best one but the one the OP can well handle and maintain …
Bookmarks