+ Reply to Thread
Results 1 to 11 of 11

VBA code to copy and paste data in one sheet from multiple sheet and loop through

Hybrid View

Farhasnat VBA code to copy and paste... 12-24-2021, 03:22 AM
BadlySpelledBuoy Re: VBA code to copy and... 12-24-2021, 03:48 AM
Farhasnat Re: VBA code to copy and... 12-24-2021, 04:22 AM
Farhasnat Re: VBA code to copy and... 12-24-2021, 04:34 AM
leonguyenz Re: VBA code to copy and... 12-24-2021, 03:56 AM
Farhasnat Re: VBA code to copy and... 12-24-2021, 04:23 AM
BadlySpelledBuoy Re: VBA code to copy and... 12-24-2021, 04:42 AM
Farhasnat Re: VBA code to copy and... 12-24-2021, 04:49 AM
BadlySpelledBuoy Re: VBA code to copy and... 12-24-2021, 05:07 AM
Farhasnat Re: VBA code to copy and... 12-24-2021, 05:39 AM
BadlySpelledBuoy Re: VBA code to copy and... 12-24-2021, 07:11 AM
  1. #1
    Registered User
    Join Date
    12-14-2021
    Location
    Bangkok
    MS-Off Ver
    MS Office 365, Verision 16.55
    Posts
    62

    Thumbs up VBA code to copy and paste data in one sheet from multiple sheet and loop through

    I am trying copy data in column B for all of the sheets for 2006-2013 to all years sheet. I tried making this code which only does for one year. Is there is any way i can automate this so that the copies data from all the sheets and paste in all years according to years. I am not really that good with VBA just learning. I want to keep using xldown for the code though as it is an requirement for my work

    Sub CreateSheet()
    
    ' Add sheet allyears
    
    Sheets.Add.Name = "allyears"
       
    End Sub
    
    
    Private Sub RunTask1()
    
    'copy data from 2006 to allyears
    
    Sheets("year2006").Activate
    Range("A11:B11").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("allyears").Activate
    ActiveCell.PasteSpecial xlPasteValues
    ActiveSheet.Select
    Columns("A").ColumnWidth = 25
    ActiveSheet.Rows.AutoFit
    
    End Sub
    Attached Files Attached Files
    Last edited by Farhasnat; 12-27-2021 at 08:38 AM.

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,911

    Re: VBA code to copy and paste data in one sheet from multiple sheet and loop through

    Maybe this?
    Sub ConsolidateData()
        Dim ws As Worksheet
        
        Sheets.Add.Name = "allyears"
        For Each ws In ThisWorkbook.Sheets
            With ws
                If ws.Name <> "allyears" Then
                    .Range("A11:B" & .Cells(Rows.Count, 1).End(xlUp).Row).Copy
                    Sheets("allyears").Cells(Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial xlPasteValues
                End If
            End With
        Next ws
    End Sub
    BSB

  3. #3
    Registered User
    Join Date
    12-14-2021
    Location
    Bangkok
    MS-Off Ver
    MS Office 365, Verision 16.55
    Posts
    62

    Re: VBA code to copy and paste data in one sheet from multiple sheet and loop through

    Thank you. This code works but the it repeats copying column A data repetedly also instead of pasting data horizontally it paste data after under let's say when pasting data for 2006 is done it put the ddata for 2007 right below it

  4. #4
    Registered User
    Join Date
    12-14-2021
    Location
    Bangkok
    MS-Off Ver
    MS Office 365, Verision 16.55
    Posts
    62

    Re: VBA code to copy and paste data in one sheet from multiple sheet and loop through

    Quote Originally Posted by BadlySpelledBuoy View Post
    Maybe this?
    Sub ConsolidateData()
        Dim ws As Worksheet
        
        Sheets.Add.Name = "allyears"
        For Each ws In ThisWorkbook.Sheets
            With ws
                If ws.Name <> "allyears" Then
                    .Range("A11:B" & .Cells(Rows.Count, 1).End(xlUp).Row).Copy
                    Sheets("allyears").Cells(Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial xlPasteValues
                End If
            End With
        Next ws
    End Sub
    BSB
    Thank you. This code works but the it repeats copying column A data repetedly also instead of pasting data horizontally it paste data after under let's say when pasting data for 2006 is done it put the ddata for 2007 right below it

  5. #5
    Registered User
    Join Date
    12-27-2012
    Location
    Việt Nam
    MS-Off Ver
    Excel 365
    Posts
    28

    Re: VBA code to copy and paste data in one sheet from multiple sheet and loop through

    I see the sheets for 2006-2013 are the same size, maybe this code:
    Sub Button1_Click()
    Dim ws As Worksheet, n%
    For Each ws In ThisWorkbook.Sheets
        If ws.Name <> "allyears" Then
            n = n + 1
            With Sheets("allyears")
                .Range("A3").Offset(0, n).Value = ws.Name
                .Range("A4:A57").Offset(0, n).Value _
                = ws.Range("B11:B64").Value
            End With
        End If
    Next ws
    End Sub
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    12-14-2021
    Location
    Bangkok
    MS-Off Ver
    MS Office 365, Verision 16.55
    Posts
    62

    Re: VBA code to copy and paste data in one sheet from multiple sheet and loop through

    Quote Originally Posted by leonguyenz View Post
    I see the sheets for 2006-2013 are the same size, maybe this code:
    Sub Button1_Click()
    Dim ws As Worksheet, n%
    For Each ws In ThisWorkbook.Sheets
        If ws.Name <> "allyears" Then
            n = n + 1
            With 0Sheets("allyears")
                .Range("A3").Offset(0, n).Value = ws.Name
                .Range("A4:A57").Offset(0, n).Value _
                = ws.Range("B11:B64").Value
            End With
        End If
    Next ws
    End Sub
    Thank you so much. this perfectly works !
    Last edited by Farhasnat; 12-24-2021 at 04:26 AM.

  7. #7
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,911

    Re: VBA code to copy and paste data in one sheet from multiple sheet and loop through

    So you want the results pasted side by side rather in one long list?

    BSB

  8. #8
    Registered User
    Join Date
    12-14-2021
    Location
    Bangkok
    MS-Off Ver
    MS Office 365, Verision 16.55
    Posts
    62

    Re: VBA code to copy and paste data in one sheet from multiple sheet and loop through

    Quote Originally Posted by BadlySpelledBuoy View Post
    So you want the results pasted side by side rather in one long list?

    BSB
    Yes side by side only the numbers as the Column A11:A64 is same for every year. Also if I could add the year headers would be great.

  9. #9
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,911

    Re: VBA code to copy and paste data in one sheet from multiple sheet and loop through

    Sub ConsolidateData()
        Dim ws As Worksheet
        
        Sheets.Add.Name = "allyears"
        
        With Sheets("year2006")
            .Range("A10:A" & .Cells(Rows.Count, 1).End(xlUp).Row).Copy
        End With
        
        Sheets("allyears").Range("A1").PasteSpecial xlPasteValues
        
        For Each ws In ThisWorkbook.Sheets
            With ws
                If ws.Name <> "allyears" Then
                    .Range("B11:B" & .Cells(Rows.Count, 1).End(xlUp).Row).Copy
                    With Sheets("allyears")
                        .Cells(2, Columns.Count).End(xlToLeft).Offset(, 1).PasteSpecial xlPasteValues
                        .Cells(1, Columns.Count).End(xlToLeft).Offset(, 1) = ws.Name
                    End With
                End If
            End With
        Next ws
        
        Sheets("allyears").Columns.AutoFit
    End Sub
    BSB

  10. #10
    Registered User
    Join Date
    12-14-2021
    Location
    Bangkok
    MS-Off Ver
    MS Office 365, Verision 16.55
    Posts
    62

    Re: VBA code to copy and paste data in one sheet from multiple sheet and loop through

    Quote Originally Posted by BadlySpelledBuoy View Post
    Sub ConsolidateData()
        Dim ws As Worksheet
        
        Sheets.Add.Name = "allyears"
        
        With Sheets("year2006")
            .Range("A10:A" & .Cells(Rows.Count, 1).End(xlUp).Row).Copy
        End With
        
        Sheets("allyears").Range("A1").PasteSpecial xlPasteValues
        
        For Each ws In ThisWorkbook.Sheets
            With ws
                If ws.Name <> "allyears" Then
                    .Range("B11:B" & .Cells(Rows.Count, 1).End(xlUp).Row).Copy
                    With Sheets("allyears")
                        .Cells(2, Columns.Count).End(xlToLeft).Offset(, 1).PasteSpecial xlPasteValues
                        .Cells(1, Columns.Count).End(xlToLeft).Offset(, 1) = ws.Name
                    End With
                End If
            End With
        Next ws
        
        Sheets("allyears").Columns.AutoFit
    End Sub
    BSB
    Thank you. I just tired to understand the code you sent understood every bit of it. Thank you !

  11. #11
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,911

    Re: VBA code to copy and paste data in one sheet from multiple sheet and loop through

    Happy I could help.

    Thanks for the rep and don't forget to mark the thread as SOLVED if you're happy you have a working solution.

    BSB

+ 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. VBA Code for Multiple sheet Pivot Table Data copy and paste to another sheet
    By neeraj1418 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-13-2021, 06:46 AM
  2. For Loop to Copy Paste Data from sheet to another
    By stein7 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-22-2019, 04:56 AM
  3. VBA code to read data and paste in other sheet using a loop.
    By aleenkhan in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-21-2018, 03:01 PM
  4. [SOLVED] How to write a code within a loop to copy and paste rows on to another Sheet
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-24-2014, 08:40 PM
  5. [SOLVED] VBA Code to copy data from one sheet and paste to multiple other sheets
    By JimmyG. in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-17-2013, 04:25 AM
  6. Replies: 1
    Last Post: 03-28-2013, 02:49 PM
  7. copy paste data with multiple selected ranges from sheet to sheet
    By sivdin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-14-2011, 02:45 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