Hi rylo,
Although your iteration limit suggestion may be an alternative solution, I don't think it'd be suitable in this instance as the code may freeze in the first instance. Maybe I'm wrong but when the code runs, it appears that way.
I can show you how with a simple macro. I basically set the MyTime value at the top of the macro, in the example here I'm adding 10 seconds from the time we start the macro. Then inside the FOR/NEXT loop(s), the first thing I do is check if the current time is greater than the stored MyTime limit. If it is, it presents a message and aborts.
So, all through your macro you need to insert this into the beginning of the loops so each time through the loops it checks the time before running its other commands.
NOTE: The MyTime variable in my sample is declared as a Static variable, so it should be available to all other macros and functions.
Once you get this to work with a short time (10 seconds in my example below) in your macro, then you can change it to 20 minutes.
Sub TimeTest() Static MyTime As Date Dim cnt As Long, j As Long, i As Long cnt = 5 'this is just so the macro has something to do MyTime = Time + TimeValue("00:00:10") For i = 1 To 2000000 If Time > MyTime Then 'This is the key test MsgBox "Time exceeded, no match found" Exit Sub End If For j = 1 To 2000000 cnt = cnt - 1 'meaningless subtraction Next j cnt = cnt + 1 'meaningless addition Next i End Sub
Last edited by JBeaucaire; 07-13-2009 at 02:06 AM.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Hi JBeaucaire,
It appears that the value of MyTime variable does not get carried over to the Functions. When I debug the code in Function, it indicates MyTime = Empty.
I have even tried inserting the whole code in Function but the it appears that MyTime is not static, it keeps changing as the loop in the Function goes to the very 1st line of the code, hence MyTime variable keeps on changing and the message will never be triggered.
Please feel free to throw in more ideas. I think your current idea should work if the value of MyTime can be carried over to the Function. I'll do some research online as well to see if I can find other solution to this problem.
Move the MyTime variable up out and above the first macro and declare it as a Public variable. That seemed to work for me:
Option Explicit Public MyTime As Date Sub TimeTest() Dim cnt As Long, j As Long, i As Long cnt = 5 'this is just so the macro has something to do MyTime = Time + TimeValue("00:00:10") For i = 1 To 2000000 If Time > MyTime Then 'This is the key test MsgBox "Time exceeded, no match found" Exit Sub End If Call NumCount(2000000) 'call the function below cnt = cnt + 1 'meaningless addition Next i End Sub Private Function NumCount(i As Long) Dim j As Long For j = 1 To i If Time > MyTime Then 'This is the key test MsgBox "Time exceeded, no match found" Exit Function End If Next j End Function
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Looking at the original macro code it is massively recursive.
Is the aim of the macro to find not only a pattern of numbers that SUM's to zero, but that also uses the maximum number of the values from the input list as possible?
Otherwise should the macro not just find "-104408" and "104408" and present you with that?
If you find the response helpful please click the scales in the blue bar above and rate it
If you don't like the response, don't bother with the scales, they are not for you
hi all,
I've had a go at modifying this (based on the existing code & the principle suggested by JB), but I haven't been successful so far (see my module called "modifiedtranslation").
In the scheme of things a small gain (in theory anyway!), is moving the use of "redim preserve" outside the loop in the initial macro & instead using a variant array to speed up the creation of the input values. This is on the basis that the initial data in the sheet should be valid (or at least validated) prior to the macro being run.
As Phil states, it is massively recursive & to see if I could find some other approaches, I Google searched for the macro's name ("Public Function Kombinationen("). I found one use of it as a proposed solution in an old monthly challenge on MrExcel.com (http://www.mrexcel.com/pc09.shtml). I've included the code of the eventual challenge winner & a runner up (Ioannis & Tushar Mehta) in separate modules but have not tried to adapt them to your spreadsheet layout. Modifying them to your file could take a while, but that would be a "one-off" time/cost & potentially they may work even better than what you currently have.
I've uploaded my sample file in case anyone's interested in investigating further using Tushar or Ioannis's code...
Goodluck,
Rob
Rob Brockett
Kiwi in the UK
Always learning & the best way to learn is to experience...
Hi Phil_V,
That is right. The reason why I've chosen to include this macro is because it uses the maximum number of the values from the input list that will sum to zero. This aim is crucial for the purpose of my objective.
Hi JBeaucaire,
fantastic work mate. It worked. Extremely impressed. Thank you so much for your help.
If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].
(Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated)
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Hi JBeaucaire,
No worries, I have already leave reputation feedback for you as soon as the macro worked.
Thank you once again.
Hi Rob,
THank you for your help too. I have seen that macro but i cannot remember why I have chosen the one that I have over the macro that you have attached. I'll still try to play around with yours again and see which one is better.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks