+ Reply to Thread
Results 1 to 9 of 9

How to re-use macro? Best way to loop and "exit" with comparison statement ?

  1. #1
    Registered User
    Join Date
    09-12-2013
    Location
    Leiria, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    11

    How to re-use macro? Best way to loop and "exit" with comparison statement ?

    Hello,

    First, I must say english is not my native language so please ignore any misspell or badly constructed phrases.

    Also I'm pretty new with VBA and macros. I checked some of the sites posted in the first sticky thread and learned some things, but many specific doubts still remain.

    1) When I say "How to re-use macro?", what I mean is: - imagine I do a macro in an excel file, and I want to give that excel file to different people. It´s supposed to those people write on the first sheet of the excel file and then run the macro, and after they do that somewhere in a row appear some values wich are useful to them. But if they are not happy with what they get, they want to put different values on the first sheet, and do this as many times as they want. Is this doable? Because when you run the macro you can´t "Ctrl+z".

    2) For the next question I have to explain moreless the problem I have in hands. Basically I have a row of values (an estimation) that after a lot of math gives me another row of values. This second row has to be inserted in the place of the first, like a loop. I'm doing this with copy paste, that I learned how to code recording a macro. Now the problem is to do the "stop", that involves some comparison statements. If you could give me some link with that kind of information, I only know it's probably with Do.. Until, but I dont know how to write this code

    3) Finally, I want to copy something but only the values. It's something like .PasteSpecial, right? Can you show me where to get more info on this matter?

    P.S.
    Question 2) code in Matlab so you can see what I have to read to learn how to do it in VBA:

    for n1 = 1:500

    eq. a
    eq. b
    eq. c
    etc..

    alpha=0.44;
    old=T;
    new=Tf;
    current=alpha*new+(1-alpha)*old;

    if and((atuais > velhos),(atuais-velhos < 0.001))
    break
    elseif and((atuais < velhos),(velhos-atuais < 0.001))
    break
    elseif (atuais == velhos)
    break
    end

    T=atuais;

    end

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: How to re-use macro? Best way to loop and "exit" with comparison statement ?

    Hi and welcome to the forum,

    1) You can't "undo" a macro with CTRL + Z but you could re-run the macro and write on the same line again.
    2) Probably a Do Until Loop like :

    Please Login or Register  to view this content.
    or

    exit a for loop with a condition
    Please Login or Register  to view this content.
    3) To paste values only use this :
    Please Login or Register  to view this content.
    Note : F1 is a good way to get help with Excel...
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Registered User
    Join Date
    09-12-2013
    Location
    Leiria, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to re-use macro? Best way to loop and "exit" with comparison statement ?

    Thanks for the answer..

    I forgot to say "atuais", "velhos", "novos" are all a column of values (an array in Matlab), so my guess is that instead of those names in the code you wrote, as to be something like:

    If (((L12:L32) > (H12:H32)) And ((L12:L32) - (H12:H32) < 0.001)) ...

    but how does excel compares them ? each value at a time like Matlab ?

    The only solution I see (but not quite what I wanted) for this with my bad skills on VBA is:

    If (((SUM(L12:L32)) > (SUM(H12:H32))) And ((SUM(L12:L32)) - SUM((H12:H32)) < 0.001)) ...

    The question 1) came up beacause there is a column of values that as to be the same each time the macro is runned, and when the loop stops those values are changed. But I think if I put a Copy/Paste of the initially required values before and outside the "loop until" code you gave me it should work ?

    Another thing is that I use two times the add-in fsolve on the code inside the loop. This has to be on the VBA code? When I run the macro it doesn't do it automatically by changing the values of the initial guesses ? If it doesn´t I guess it has to be inside the VBA code. I'm asking because right now my fsolve is giving a feasibility error and I can´t test this.
    Last edited by varreddor; 09-13-2013 at 11:49 AM.

  4. #4
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: How to re-use macro? Best way to loop and "exit" with comparison statement ?

    1)
    To compare the 2 columns, you need to compare value by value, like this :
    (note : I changed your condition, using the absolute value seems simpler)

    Please Login or Register  to view this content.

    2) your suggest to copy/paste the initial values should work.

    If you still have problem, please post a sample workbook.

  5. #5
    Registered User
    Join Date
    09-12-2013
    Location
    Leiria, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to re-use macro? Best way to loop and "exit" with comparison statement ?

    Another thing is that I use two times the add-in fsolve in sheet "Rad" and "Geral". I have the feeling it has and it's gonna be tricky to put it working on VBA, because on excel it works but I have to go to the add-in and do solve. I was hoping to have that doing inside the loop of VBA.

    When I run the macro it gives an error and I think it's because the subtraction (If Abs(atuais(j) - velhos(j)) < 0.001 Then Exit For) is giving a value zero and there is no condition for that on the code, but it's not supposed to give zero anyways, that's happening because of the fsolves not working I guess.

    By the way.. many thanks for the help until now, I think "we" are almost there
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: How to re-use macro? Best way to loop and "exit" with comparison statement ?

    When you pass a range to an array, the arrays has 2 dimensions.
    So try this instead :

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    09-12-2013
    Location
    Leiria, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to re-use macro? Best way to loop and "exit" with comparison statement ?

    thanks, now there is no error. Well, but now the code is doing the copy paste thing and the loop, but the values are exactly the same from the beginning because the add-in I use to change the values, the f-solve, is not doing anything. Is there a way for me to put it inside the code, or the only way is at each iteration go to tab Data - Analysis - Solver, and do it "manually" ?!

  8. #8
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: How to re-use macro? Best way to loop and "exit" with comparison statement ?

    I have used Solver with VBA much, but this link could help :

    http://peltiertech.com/Excel/SolverVBA.html

  9. #9
    Registered User
    Join Date
    09-12-2013
    Location
    Leiria, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to re-use macro? Best way to loop and "exit" with comparison statement ?

    I again, had to work on other things but now im back to VBA. The link GC Excel gave it's quite good, explains many interesting things but again I got an error, and to some users it may be a common one and hopefully someone can help me. I took a printscreen, but I can also put my workbook updated with my archaic VBA. For now here's the pic.
    fsolve error.jpg

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] "Unload Me" will not exit a Do While Loop
    By jcaskey in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-01-2013, 12:25 PM
  2. if Range("A1") = "Mike" Then Range("A1") = "michael" Then Exit Sub
    By a8015945 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-30-2013, 10:08 AM
  3. [SOLVED] How to trigger an "Exit For" command from a For Loop, when a cell in another workbook....
    By Rusty_The_Kid in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-09-2013, 12:04 AM
  4. Replies: 3
    Last Post: 12-14-2006, 01:36 PM
  5. Call a sub statement in "Personal Macro Workbook" from "ThisWorkbo
    By QC Coug in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-26-2005, 03:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1