+ Reply to Thread
Results 1 to 21 of 21

Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & looping

  1. #1
    Registered User
    Join Date
    03-19-2013
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    12

    Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & looping

    Hello Gurus... This is my first post and I registered to this forum since I needed some special help. I've been a long time excel user but hardly write macros. I figured I coulduse your expertse on this issue - I wll cut to the chase and define the problem -

    Here is my problem - I am trying to supply inputs from sheet1 to sheet2 on a workbook and get the output (based on a formula) and put it back on sheet1 in a specified range of cells and loop this entired process 700 times.
    Process -
    step1 - I would like to copy values in cells A1,C1,E1,G1,I1,K1,M1 from Sheet1 and paste them into B12:B18 (transposed) on sheet 2.

    step 2 - Similarly, I would like to copy values in cells B1,D1,F1,H1,J1,L1 and N1 from sheet 1 and paste them into C12:C18(transposed) on sheet 2

    Step 3 - Then I would like to grab/copy (calculated output values) from cells C22:C27 from sheet 2 and paste it back to sheet 1 into cells AB1:AG1 (transposed)

    Step 4 - I would like the code to repeat steps 1 2 and 3, in that order, for the values in row 2 (A2), row 3, row 4 and keep doing it one by one uptill row 700.. bacially loop it. Please note that I will be copying values from sheet 1 from different rows each time sequentially but pasting it on the same cells in sheet 2 and pulling the output also from the same cells defined above in step 3

    below is what I have but it is not performing as intended - it is unfinished though



    Option Explicit
    Sub MyCopy()
    Dim lngLastRow As Long
    Dim lngRow As Long
    lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    For lngRow = 1 To lngLastRow

    'step 1
    Range("A1,C1,E1,G1,I1,K1").Select
    Range("K1").Activate
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Import").Select
    Range("P1").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=True
    'Step 2
    Sheets("Sheet1").Select
    Range("B1,D1,F1,H1,J1,L1").Select
    Range("L1").Activate
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Import").Select
    Range("Q1").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=True
    'Step 3
    Range("O12:O18").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet1").Select
    Range("N1").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=True

    Next lngRow
    End Sub

    Please note that some cell references aboe are not precise but you get my point...
    Also i would like to introduce some time delay in milliseconds before step 3.... any code reocommended ?
    Can some gurus please suggest an efficient way or even a different piece of elegent code to do it. I am an infrequent user of excel vba macro and I am just taking up time to come up with it.

    I have attached a sample spreadsheet with the input and output fields.

    Thank you for your help! Please let me know ifyou have questions.
    Last edited by Chaipau; 03-21-2013 at 10:13 AM.

  2. #2
    Registered User
    Join Date
    03-19-2013
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    Sorry Just bumping this since I edited my post a little

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    Your request on this line is not clear

    Step 3 - Then I would like to grab/copy (calculated output values) from cells C22:C27 from sheet 2 and paste it back to sheet 1 into cells AB1:AG1 (transposed).
    I am not sure if you want to copy the entire range C, or just 7 rows. Try it anyway

    Please Login or Register  to view this content.
    Last edited by AB33; 03-20-2013 at 01:29 PM.

  4. #4
    Registered User
    Join Date
    03-19-2013
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    Thanks for the reply AB33! Your code is working But it is not peforming as expected. Below are the problems -

    1.For Step 3, to answer your question, it is just those 7 rows - C22:C27. Also, For step 3, I would like it to be looped 700 times - as many times as steps 1 and 2. So in the first iteration it should take values from C22:C27 and put them back into sheet 1 AB1:AG1, on the next iteration it should take values from C22:27 and put them back to AB2:AG2. On the third iteration it should take values from C22:27 and put them back to AB3:AG3. On the fourth iteration it should take values from C22:27 and put them back to AB4:AG4. You get the drift...

    Basically, sheet 1 is my inputsheet from where I grab each row of values and paste it on sheet2 on the SAME set of cells each time. And then I pull/copy my "output" values from cell C22:C27 EACH time and put it back to sheet 1 in the specified rows.
    so each input row from sheet 1 has a corresponding output row on sheet 1 (pasted in AB:AG) incrementally.

    2. For STEP 2, I would like to copy values in cells B1,D1,F1,H1,J1,L1 and N1 from sheet 1 and paste them into C12:C18(transposed) on sheet 2. - Fo every single iteration I would like the values to be pasted on the SAME cells (C12:C18(transposed) on sheet 2) NOT IN DIFFERENT cells like you have done in the code.

    3. I would like to add a few milliseconds of delay before step 3 is executed for each iteration.

    Please advise with appropriate code. Thanks again!!

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    "For STEP 2, I would like to copy values in cells B1,D1,F1,H1,J1,L1 and N1 from sheet 1 and paste them into C12:C18(transposed) on sheet 2. - Fo every single iteration I would like the values to be pasted on the SAME cells (C12:C18(transposed) on sheet 2) NOT IN DIFFERENT cells like you have done in the code."


    Sorry! I am not getting it. When you. So say loop through, you are looping from row 1 to row 24 ,one at a time, so how can copy 24 times in to single range of C12-C18? Or are you just copying a single row which is row 1. When you transpose a range you swap horizontal to vertical and vice versa. In your case, the data in source are in horizontal and will change in to vertical.

  6. #6
    Registered User
    Join Date
    03-19-2013
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    No Problem. Let me explain. Yes I would like to loop through rows 1 through 24 (or 700) one at a time and this is how it should work -
    First, Think of sheet1 as a data feeder sheet. Think of Sheet 2 as the magical engine/application with a set of input(B12:B18 and C12-C18) and output fields (C22:27) so you pass your data from sheet 1 to sheet 2, you grab the outputs (C22:27) and put it back to sheet 1(AB:AG)

    NOW -
    1.You copy row 1 data first(from sheet 1 range A1:N1) into range of B12:B18 and C12-C18 respectively. NOW AFTER COPYING these, You go grab the values from C22:27 on sheet 2 and paste them back to sheet1 in rows AB1:AG1
    2.Then, You copy row 2 data(from sheet 1 range A2:N2) into range of B12:B18 and C12-C18 respectively. NOW AFTER COPYING these, You go grab the values from C22:27 on sheet 2 and paste them back to sheet1 in rows AB2:AG2
    3.Then, You copy row 3 data(from sheet 1 range A3:N3) into range of B12:B18 and C12-C18 respectively. NOW AFTER COPYING these, You go grab the values from C22:27 on sheet 2 and paste them back to sheet1 in rows AB3:AG3
    4.Then, You copy row 3 data(from sheet 1 range A4:N4) into range of B12:B18 and C12-C18 respectively. NOW AFTER COPYING these, You go grab the values from C22:27 on sheet 2 and paste them back to sheet1 in rows AB4:AG4
    5.Then, You copy row 3 data(from sheet 1 range A5:N5) into range of B12:B18 and C12-C18 respectively. NOW AFTER COPYING these, You go grab the values from C22:27 on sheet 2 and paste them back to sheet1 in rows AB5:AG5
    So on and so forth until 700 rows are completed....


    Question - what is NR = 12 in your code ???

    I would like to add a few milliseconds of delay before step 3 is executed for each iteration.

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    Question - what is NR = 12 in your code ???

    I would like to add a few milliseconds of delay before step 3 is executed for each iteration.

    N is there the first row in sheet 2.
    I would like to add a few milliseconds of delay before step 3 is executed for each iteration.
    Sorry! I do not know. You could post it as a new thread.


    1.You copy row 1 data first(from sheet 1 range A1:N1) into range of B12:B18 and C12-C18 respectively. NOW AFTER COPYING these, You go grab the values from C22:27 on sheet 2 and paste them back to sheet1 in rows AB1:AG1
    Okay! You want to copy C22:27 24 times too. Whether I post it separately, or incorporated with the code, it will not make any difference, you want to match the number of times you wish to copy that range is the same as the other loop.
    Now, How can you copy all 24 rows in to the same range B12:B18 and C12-C18 ?

    sheet 1 range A1:N1 will be copied in to B12:B18 and C12-C18 and then
    range A2:N2 will be copied in to next empty cells, unless you want to cut the already copied range.
    I am missing something. Where are all 24 rows will be copied in to sheet2?

  8. #8
    Registered User
    Join Date
    03-19-2013
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    YES, I want to first cut or clear the earlier copied range from row 1 and then copy the range from row 2 into B12:B18 and C12-C18. repeat this process 24 times. What will be the code for that ?

    I have modified your earlier code now and this is how it looks - Please run it to see how it works now

    Please Login or Register  to view this content.
    Last edited by Chaipau; 03-20-2013 at 04:19 PM.

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    Please use code tags with your code. It is forum's rule to use tags with your code. Look at my code, they all wrapped in code tags. Could you please edit the above code while I look at the thread? Click edit, highlight the entire code, then click this sign # from the quick reply.

  10. #10
    Registered User
    Join Date
    03-19-2013
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    Ok. Done. Thanks. Since I am a newbie I was not aware or read the guidelines.

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    Now back to start!
    You do not need NR as my original code assumed that you want to copy 24 rows next to each other. Since you are not, no need to use next row.
    As I copy and cut range C22-C28, sheet1 AB will be populated by the new range in C22-28. That is A1-N1 will be copied in C22-28 and cut it and copy back to sheet1 in the next Range AB.
    Where I am lost is what is the relevance of copying B-12-B18 and C12-C18, if we are simply cut them and will not be copied in to any where? What is the point of copying and cutting them? My understanding from the attached is you have data in sheet 2 range C22-28 and these are copied from the range above C12-C18.
    I think if you are trying to link them, perhaps as you said, you need a code with timer delay.
    Sorry! I could not helpful as I am probably not getting your request.

  12. #12
    Registered User
    Join Date
    03-19-2013
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    Okay. I fixed that too now. Try code below it works. BUT I see that both your sheet 1 and sheet 2 code loops are independent. I want steps 1, 2 and 3 to occur one after the other in that sequential order.
    how do I include the "with ms..." function and make it a part of the earlier step 1 and 2 loop ? Can you modify the code below and paste it back after making the changes ?

    Please Login or Register  to view this content.

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    No, there is something missing from the code. As you get rid off the next NR=NR+1, the code stuck at 12 and did not go to C22, hence nothing is copied in to AB sheet1 as C22-28 range is blank.
    You either change the N=12 to N=22, or out back the next row loop.
    Here is all lines of code in one

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    03-19-2013
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    Well thanks again but this code you pasted seems to hang up after iteration 3 and does not go any further. On debugging it seems like it gets stuck here:

    Please Login or Register  to view this content.
    did you try running it at your end ? I am attaching a sample spreadsheet you can use to run it on.

  15. #15
    Registered User
    Join Date
    03-19-2013
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    attached book4.xls
    Attached Files Attached Files

  16. #16
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    Yes, you are right!
    N is fixed at 12 and not increasing with the loop, so despite the code loops through 300 times, NR remains static 12, so the code will end up copying the last batch as all previous batches have been overwritten.
    It is the same issue with the attached

  17. #17
    Registered User
    Join Date
    03-19-2013
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    the fact that previous batches get overwritten is NOT a problem for me. What is the problem is the output(C22:C27) does not get copied and pasted to sheet 1 (AB:AG) 300 times. Why does it stop after row 3. How do you fix it ? ideally AB300:AG300 should be populated with output1, output2.... Also if you look at B12:B18 and C12:C18 on sheet2, they should reflect the valus in row 300 of sheet 1 (A300:N300) but they dont. How do you fix that ?

  18. #18
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    No, N has nothing to do with the bit (C22:C27) of the code

    Please Login or Register  to view this content.
    These lines do not have N. What these lines do is to copy range c22-27 300 times in the next empty row. First batch will go in AB2-AH2, then the next copy will go in to AB3-AH3 and so on. Why do not you put some figures in the range c22-27 and run the code?

  19. #19
    Registered User
    Join Date
    03-19-2013
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    Quote Originally Posted by AB33 View Post
    No, N has nothing to do with the bit (C22:C27) of the code

    Please Login or Register  to view this content.
    These lines do not have N. What these lines do is to copy range c22-27 300 times in the next empty row. First batch will go in AB2-AH2, then the next copy will go in to AB3-AH3 and so on. Why do not you put some figures in the range c22-27 and run the code?
    Yes, That is what I want. BUT It does not do that. See the book4.xls I attached.I've put some figures in the range c22-27 and ran the code but on sheet1 it only increments till AB3-AH3 and not after that. That is the whole problem. You can take the code and run it agaianst book4.xls I attached and see how many times it increments.

  20. #20
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    Please do not reply with quote. Quotes have no purpose but only to clutter the space and make me to vomit.

    I have tried this code by inserting dummy data in range c22-c27 of sheet 2 and copies 300 times

    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    03-19-2013
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Need help with defining VBA Macro for copypaste from sheet1 to sheet2 and back & loopi

    Edited. No more questions for now. Thanks!!
    Last edited by Chaipau; 03-21-2013 at 10:58 AM.

+ 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.6.0 RC 1