+ Reply to Thread
Results 1 to 32 of 32

VBA code to copy, paste and transpose every 5th row from multiple worksheets

  1. #1
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Hello - I have a range of values in one tab called "Debt" and I want to tranpose the data into rows starting in cell C2 on my "Statatest" tab. However, I would like to paste the data on every fifth row. I cannot figure out how to do this my current code only pastes and transposes to one row. In all, I have five worksheets with data I want to copy and transpose to the Statatest tab. Any thoughts are greatly appreciated?

    As you can see from the code I am a VBA novice...

    Here is my code:

    Sub test()
    Sheets("Statatest").Select
    Columns("C:C").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.ClearContents
    Range("C1").Select
    Sheets("Data").Select
    Sheets("Statatest").Select
    Sheets("Statatest").Move After:=Sheets(5)
    Sheets("Productivity").Select
    Range("B6:B1000").Select
    Range("B6").Activate
    Selection.Copy
    Sheets("Statatest").Select
    Range("C1").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    False, transpose:=True
    Sheets("Debt").Select
    Range("AE10").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Statatest").Select
    Sheets("Statatest").Select
    Range("C2").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    False, transpose:=

    End Sub

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    What ranges do you need to transpose on each sheet. From your code it looks like you are doing B6:B1000 on the productivity sheet and AE10 to "the end" on the Debt sheet. What about the other sheets. Please clarify what ranges you need copied on what sheet.

  3. #3
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    The productivity sheet is the dates used for the time series. The data I need to copy and transpose from the other sheets also begins on the AE10 cell and includes to the end (down and to the right). I can email you a screen shot if needed.

  4. #4
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Any thoughts? The most important part of the loop is to paste and transpose the data every fifth row. Any help is greatly appreciated.

  5. #5
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    What ranges do you need to transpose on each sheet?

    An example answer would be;

    Sheet "Productivity" Range B6:B1000
    Sheet "Debt" Range AE10 to the last row, and every column to the right of column AE


    Pasting on every 5th row is easy, i just want to make sure I know which ranges you want copied.

  7. #7
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    The data will update so I hope to have a dynamic code. See below. Thanks!

    Sheet "Productivity" B6 --> all down until data ends (this is for the dates, which begin in C1 in the destination sheet)
    Sheet "Debt" AE10 all down and every column to the right of AE (all down and all to right) until data ends
    Sheet "Productivity" C6 all down and every column to the right of AE (all down and all to right) until data ends
    Sheet "Terms of Trade" AE10 all down and every column to the right of AE (all down and all to right) until data ends
    Sheet "REER" AE10 all down and every column to the right of AE (all down and all to right) until data ends
    Sheet "FX" AE10 all down and every column to the right of AE (all down and all to right) until data ends

    Essentially, I am shaping my data to upload into Stata in a panel format. On the destinattion sheet Cell A1 is my label for country and B1 is the variable name. C2 is where I transpose the dates. My First country is Australia so A2 to A6 are all Australia. B2 to B6 are debt, productivity, terms of trade REER and FX from each of the worksheets. I need to past every fifrth row since Canada debt is C7.

  8. #8
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Sorry C1 is where I transpose the dates. The first variable is transposed into C2, which is Australia debt from sheet "Debt"

  9. #9
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Canada debt on the sheet "Debt" will be transposed in C7 and euro zone debt in C12. I prefer to have the loop run through the data set (5 worksheets) and copy and transpose into my upload sheet (Statatest).

    I hope this is clear. Thanks again.

  10. #10
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Ok i think i got it. Gonna take me a little bit to code and test.

  11. #11
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Let me know how this works. Hopefully I didn't miss anything:

    Please Login or Register  to view this content.
    Edit: Just a thing to note. When transposing you always have to consider that excel allow more rows than columns. I could have put a check in to account for that but I assumed you didn't have too many rows.
    Last edited by stnkynts; 11-07-2013 at 03:18 PM.

  12. #12
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Great, thanks. It copies and transposes the dates from Productivity but none other data is copied and pasted into the Statatest sheet.

  13. #13
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    yeah that my fault. i realized a small error in the code, try this:

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Does the set ws1 = sheets need to include the other sheets - Debt, Terms of Trade, REER and FX?

  15. #15
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Quote Originally Posted by cog38 View Post
    Does the set ws1 = sheets need to include the other sheets - Debt, Terms of Trade, REER and FX?
    No, the other ones should be covererd under the Case Else, since it is all the same actions in each of those sheets. However, I put it into a Select/Case format so in the future if you wanted to make special accomidation for a sheet you could easily specify it. Let me know if the above amended code works.

  16. #16
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    I see thanks. The code works but I not sure what data it is pullling in. My data are based on links from Bloomberg and the data pasted from the code leaves an error. Would you prefer I emailed you a copy of the sheet?

  17. #17
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    I cannot post to the website due to restrictions.

  18. #18
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Furthermore, here is the sequence of the loop. Copy and paste from "Debt" AE10 (down and to the right) in this case to AS45 and transpose in Statatest C2 in every five rows until C72. Next, Copy and paste from "Productivity" C6 (down and to the right) in this case to Q48 and transpose in Statatest C3 in every five rows until C73. Copy and paste from "Terms of Trade" AE10 (down and to the right) in this case to AS45 and transpose in Statatest C4 in every five rows until C74. Same loop for sheets "REER" and "FX" which are pasted in C5 and C6 and finish at C76. Does that help?

  19. #19
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Make an example workbook and as clearly as possible show (in the workbook) what you want to go where.

  20. #20
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    attached is the sheet.
    Attached Files Attached Files

  21. #21
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Statatest is the destination sheet. In this tab I need the dates (from productivity) to be pasted in C1. Debt contains the debt variable for each country. As you can see each "variable" on statalist relates to a "sheet" with the data for each country. Barrring Productivity the data points I want to copy and paste into Stattest begins AE10. I do not plan to copy the data from CAB. I hope this helps.

  22. #22
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    There is no "Statatest" sheet on your example workbook. Make a sheet called Statatest, copy columns AB from "Sheet6" (so it looks nice) and run this code. It matches what you had on sheet 6.

    Please Login or Register  to view this content.

  23. #23
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Great, thanks! This works perfectly. My apologies but I sent an older version of the sheet. Will this code work for the sheets that I need to collect data beginning in Cell AS35 instead of B10. For instance, the Gross Government Debt tab has the necessary data in AS10 instead of B10. Will this code still work with that in mind? I will play with the sheet tomorrow. Can I follow up with any further questions? Thanks again.

  24. #24
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    Here is the updated Excel sheet. I tried to adjust the code with some of the changes I made from the prior sheet to no avail. Can you please update the code you posted? Your help has been greatly appreciated.
    Attached Files Attached Files

  25. #25
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    It is hard for me not to be snarky in this situation but here you go:

    Please Login or Register  to view this content.

  26. #26
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    I do apologize for any inconvenience. As I mentioned, you have been a great help. I have one final question. I think the macro is pasting incorrect data points since I am getting #ref errors for most of the cells. I think it has to do with my API links to Bloomberg. How can I change the code so for sheets Debt, Terms of Trade, REER and FX (Not Productivty) to grab all the data down and to the right of cell AE10 instead of B10?

  27. #27
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    This shows the error and I will also attach a screen shot.
    Attached Files Attached Files

  28. #28
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    excel_screen.jpg

    This shows the "debt" sheet. How can I adjust the code to copy and paste the data that begins in cell AE10 for Debt, Terms of trade, REER and FX?

  29. #29
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    It is because of the formula references in each sheet. We can make a simple change from pasteall to pastevalues. I believe that may correct it (Note: It is pulling data in column AE and every column to the right)

    Please Login or Register  to view this content.
    Last edited by stnkynts; 11-08-2013 at 11:56 AM.

  30. #30
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    That worked perfectly! All set. And thank you for not being snarky. This is extremely helpful...

  31. #31
    Registered User
    Join Date
    11-07-2013
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    hi - I have a follow up about this code. can I reply in this message thread?

  32. #32
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code to copy, paste and transpose every 5th row from multiple worksheets

    I can't remember what I had for dinner last night, let alone what was going on in 2013 . What are you wanting to know?

+ 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] VBA Copy Paste Transpose Loop (Multiple Sheets)
    By demon8991 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-03-2013, 11:50 PM
  2. [SOLVED] Please help me to modify the code copy/paste/transpose
    By tuongtu3 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-30-2013, 09:44 AM
  3. [SOLVED] What is the most efficient way to copy, paste and transpose values between worksheets?
    By fredrs05 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-18-2013, 03:51 PM
  4. Code to Copy & Paste Transpose Selected Information
    By tiger01 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-17-2011, 01:02 AM
  5. Replies: 1
    Last Post: 05-11-2011, 11:07 PM

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