+ Reply to Thread
Results 1 to 9 of 9

Thread: Copy, paste, calculate and loop

  1. #1
    Registered User
    Join Date
    03-31-2010
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    38

    Copy, paste, calculate and loop

    Hello there.

    Am in need of some assistance to create a macro that will be an amazing time saver.
    I have built this sample file for the sake of simplicity. If anyone can assist me with this task on this spreadsheet then I can recode it to work in the real file.
    The real file is a very hefty tool for testing financial data.

    Here are the steps I need the macro to perform:

    With the file open and calculation set to manual, I need the macro to:

    1. Copy cells from worksheet !VARIABLES C23:33
    2. Paste Special > Values into worksheet !TESTER C10:C20
    3. Calculate (in the real file, this can take up to 20 seconds)
    4. After calculation is complete, COPY from worksheet !TESTER C23:C33
    5. Paste > Values into worksheet !RESULTS C23-C33

    Then, it should LOOP back to step 1 and repeat, except that:

    Step 1 Should copy the next column over inside the VARIABLES worksheet (ie D23:D33, E23:E33, and so on) every time this loops until it reaches the cell that says "END" in $V$10
    Step 5 Should copy the next column over inside the RESULTS worksheet (ie D23:D33, E23:E33, and so on) every time until the macro ends because the condition in step 1 is met ("END" found in $V$10)

    Steps 2-4 are exactly the same.

    Again, this is a sample file and is purely for functionality.
    I really appreciate any assistance I can find on this.

    Thanks again!

    William
    Attached Files Attached Files
    Last edited by williampdx; 01-11-2012 at 05:43 PM. Reason: SOLVED

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Macro needed to copy, paste, calculate and loop until complete

    This should do it:

    Option Explicit
    
    Sub RunTests()
    Dim TestRNG As Range, Tst As Range, Col As Long
    
    Set TestRNG = Sheets("Variables").Rows(10).SpecialCells(xlConstants, xlNumbers)
    Sheets("Results").Range("C23:EE33").Value = ""
    Col = 3
    
    For Each Tst In TestRNG
        Sheets("Tester").Range("C10").Resize(11).Value = Tst.Resize(11).Value
        Sheets("Results").Cells(23, Col).Resize(11).Value = Sheets("Tester").Range("C23").Resize(11).Value
        Col = Col + 1
    Next Tst
    
    Sheets("Tester").Range("C10").Resize(11).ClearContents
    
    End Sub
    Last edited by JBeaucaire; 01-11-2012 at 10:56 AM. Reason: Correction, colored
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon 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!)

  3. #3
    Registered User
    Join Date
    03-31-2010
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    38

    Re: Macro needed to copy, paste, calculate and loop until complete

    Let me be more clear.
    I need it to copy the results from C23:C33 from !TESTER sheet so that when it is done the !RESULTS page would be populated with:

    3 oranges
    3 apples
    2 grapes

    etc....

    As if now, it is just copying over #'s.

    Other than that, the macro works swimmingly.

    Can you help me to get this to work?

    William
    Last edited by williampdx; 01-11-2012 at 10:35 AM. Reason: mistake in range

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Macro needed to copy, paste, calculate and loop until complete

    Check the code above again, I made one tiny correction, it is highlighted for you. My apologies.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon 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!)

  5. #5
    Registered User
    Join Date
    03-31-2010
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    38

    Re: Macro needed to copy, paste, calculate and loop until complete

    I tried the code above and it copies the correct range, but it is not calculating before each step.
    It should be performing a calculation so that after calculating in !TESTER C23:C33, it moves the results of those calculation to the columns in !RESULTS C23:U33. It should look like:

    see imageresults.jpg


    Does that make sense?

    William
    Last edited by williampdx; 01-11-2012 at 11:30 AM.

  6. #6
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Macro needed to copy, paste, calculate and loop until complete

    Excel normally calculates on its own. Is your "calculation mode" set to manual?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon 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!)

  7. #7
    Registered User
    Join Date
    03-31-2010
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    38

    Re: Macro needed to copy, paste, calculate and loop until complete

    You are correct in that if I set the calculation to automatic, the macro works perfectly and does everything I need it to do.
    But, on the file I am going to modify this macro to work on, I need excel to manually calculate.
    How can we modify the macro to calculate at the appropriate times to give me the results I need with the file set to manual calculation?
    Application.CalculateFull somewhere????

    Thanks!

  8. #8
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Macro needed to copy, paste, calculate and loop until complete

    Sure, can you not take a guess where? Take a guess and insert it... let me know if you get it.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon 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!)

  9. #9
    Registered User
    Join Date
    03-31-2010
    Location
    Portland, OR
    MS-Off Ver
    Excel 2007
    Posts
    38

    Re: Macro needed to copy, paste, calculate and loop until complete

    Thanks to you, I have figured every piece of this out and have already adapted this to my other sheet.
    I made adjustments for a manual calculation at what I think is the appropriate time for it.

    Sub RunTests()
    
    Dim TestRNG As Range, Tst As Range, Col As Long
    
    Set TestRNG = Sheets("Variables").Rows(20).SpecialCells(xlConstants, xlNumbers)
    Sheets("Results").Range("C23:EE34").Value = ""
    Col = 3
    
    For Each Tst In TestRNG
        Sheets("Tester").Range("C10").Resize(12).Value = Tst.Resize(12).Value
        Application.CalculateFull
        Sheets("Results").Cells(9, Col).Resize(12).Value = Sheets("Tester").Range("C31").Resize(12).Value
        Col = Col + 1
        
    Next Tst
    
    Sheets("Tester").Range("C10").Resize(12).ClearContents
    
    End Sub
    Thanks again for all your help. This will save me hundreds of hours of work!

    William

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0