+ Reply to Thread
Results 1 to 14 of 14

Copy-Paste different cells from each worksheet to another worksheet (excluding some)

Hybrid View

  1. #1
    Registered User
    Join Date
    10-03-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    98

    Copy-Paste different cells from each worksheet to another worksheet (excluding some)

    Hello VBA Experts

    I have a workbook which contains 50+ worksheets,
    I want to copy Cells B1, K1 and L1 from each worksheet and paste it another worksheet ("OtherDatadump").
    Also note that from this workbook few worksheets need to be excluded. These are with the names (Roles&Dropdowns, OtherDatadump, Summary, Consolidated Data, Project-SampleTemplate) in my workbook.
    Can someone please help me?

    I tried with below code, but this is not giving me desired results (For now I just tried to copy-paste cell K1).

    Sub CopyPasteValuesFromAllWorkSheets()
    
      Dim ws As Worksheet
      Dim wb As Workbook
      Dim lastrow1 As Long, lastrow2 As Long
      
      Set wb = ThisWorkbook
    
    
      For Each ws In wb.Worksheets
           
         If ws.Name <> "Roles&Dropdowns" And ws.Name <> "OtherDatadump" And ws.Name <> "Summary" And ws.Name <> "Consolidated Data" And ws.Name <> "Project-SampleTemplate" Then
        
           ws.Range("K1").copy
           lastrow1 = ThisWorkbook.Sheets("OtherDatadump").Range("H" & Rows.Count).End(xlUp).Row
           ThisWorkbook.Sheets("OtherDatadump").Range("I2" & lastrow1).PasteSpecial Paste:=xlPasteValues
        
        End If
    
    'NextIteration:
      Next ws
    
      Application.ScreenUpdating = True
    
      End Sub

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Copy-Paste different cells from each worksheet to another worksheet (excluding some)


    Hi,

    as we do not have this workbook …
    Last edited by Marc L; 09-02-2023 at 09:41 AM.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,470

    Re: Copy-Paste different cells from each worksheet to another worksheet (excluding some)

    This
    ... .Range("I2" & lastrow1). ...
    should probably be this
    ... .Range("I" & lastrow1). ...
    But try this:

    Sub CopyPasteValuesFromAllWorkSheets()
    
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim lastrow1 As Long, lastrow2 As Long
    
    Set wb = ThisWorkbook
    
    Application.ScreenUpdating = False
    For Each ws In wb.Worksheets
        If ws.Name <> "Roles&Dropdowns" And ws.Name <> "OtherDatadump" And ws.Name <> "Summary" And ws.Name <> "Consolidated Data" And ws.Name <> "Project-SampleTemplate" Then
            With wb.Sheets("OtherDatadump")
                lastrow1 = .Range("H" & .Rows.Count).End(xlUp).Row
                .Range("I" & lastrow1).Value = ws.Range("K1").Value
            End With
        End If
    Next ws
    Application.ScreenUpdating = True
    
    End Sub
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,470

    Re: Copy-Paste different cells from each worksheet to another worksheet (excluding some)

    Note that you are using column H to determine the last row but writing to column I.

    So, maybe it should be this:

                lastrow1 = .Range("I" & .Rows.Count).End(xlUp).Row
                .Range("I" & lastrow1 + 1).Value = ws.Range("K1").Value

  5. #5
    Registered User
    Join Date
    10-03-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    98

    Re: Copy-Paste different cells from each worksheet to another worksheet (excluding some)

    The code which was supplied not producing the desired results. I am attaching my workbook maybe this helps in debugging and producing the expected results.

    I am attaching screenshot with final results, how the final result should look like. I request to please modify the code (or write a new code accordingly). Thanks in advance!!
    Screenshot-9.PNG
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Copy-Paste different cells from each worksheet to another worksheet (excluding some)


    So why some worksheets are hidden, could the criteria be to include only visible worksheets after result worksheet ?

  7. #7
    Registered User
    Join Date
    10-03-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    98

    Re: Copy-Paste different cells from each worksheet to another worksheet (excluding some)

    Criteria or requirement for script is to exclude the worksheets which are with the names: Roles&Dropdowns, OtherDatadump, Summary, Consolidated Data, Project-SampleTemplate but include rest all others.

    The reason for some sheets to be hidden is NOT to expose these to all users.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this ...


    According to post #5 attachment - missing in initial post ! - a starter VBA demonstration
    to paste only to Sheet8 (OtherDatadump) worksheet module :

    PHP Code: 
    Sub Demo1()
            
    UsedRange.Offset(1).Rows.Delete
            Application
    .ScreenUpdating False
        
    For S& = Index 1 To Worksheets.Count
        With Worksheets
    (S)
            If .
    Visible = -1 Then Rows(UsedRange.Rows.Count)(2).Columns("C:F") = Application.Index(.[A1:K1], 1, Array(21157))
        
    End With
        Next
            UsedRange
    .Borders.Weight 2
            Range
    ("E2:E" UsedRange.Rows.Count).NumberFormat "mmm-yyyy_W"
            
    Application.ScreenUpdating True
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Try this !


    According to previous post a ThisWorkbook module VBA demonstration for starters :

    PHP Code: 
    Sub Demo2()
        
    Dim S$(), Ws As Worksheet
            S 
    Split("1 4 5 7 8")
       
    With Sheet8
           
    .UsedRange.Offset(1).Rows.Delete
        
    For Each Ws In Worksheets
         
    If IsError(Application.Match(Replace$(Ws.CodeName"Sheet"""), S0)) Then _
           
    .Rows(.UsedRange.Rows.Count)(2).Columns("C:F") = Application.Index(Ws.[A1:K1], 1, Array(21157))
        
    Next
           
    .UsedRange.Borders.Weight 2
           
    .Range("E2:E" & .UsedRange.Rows.Count).NumberFormat "mmm-yyyy_W"
       
    End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,470

    Re: Copy-Paste different cells from each worksheet to another worksheet (excluding some)

    Try

    Sub CopyPasteValuesFromAllWorkSheets()
    
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim lastrow1 As Long, lastrow2 As Long
    
    Set wb = ThisWorkbook
    
    Application.ScreenUpdating = False
    For Each ws In wb.Worksheets
        If ws.Name <> "Roles&Dropdowns" And ws.Name <> "OtherDatadump" And ws.Name <> "Summary" And ws.Name <> "Consolidated Data" And ws.Name <> "Project-SampleTemplate" Then
            With wb.Sheets("OtherDatadump")
                lastrow1 = .Range("C" & .Rows.Count).End(xlUp).Row
                .Range("C" & lastrow1).Value = ws.Range("B1").Value
                .Range("D" & lastrow1).Value = ws.Range("K1").Value
                .Range("E" & lastrow1).Value = ws.Range("E1").Value
                .Range("F" & lastrow1).Value = ws.Range("G1").Value
            End With
        End If
    Next ws
    Application.ScreenUpdating = True
    
    End Sub

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Copy-Paste different cells from each worksheet to another worksheet (excluding some)


    Hi TMS !

    Logic error in your last VBA procedure, did you test it as it is ?

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,470

    Re: Copy-Paste different cells from each worksheet to another worksheet (excluding some)

    Hi Marc. Not tested at all. Looking at pictures, and playing with code on my iPad .

    But please feel free to correct it on my behalf . . . I can’t see it

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Copy-Paste different cells from each worksheet to another worksheet (excluding some)


    Ok so post #10 correction :
    Sub CopyPasteValuesFromAllWorkSheets()
    
    'Dim wb As Workbook     ' useless !
    Dim Ws As Worksheet
    Dim lastrow1 As Long    ' useless -> , lastrow2 As Long
    
    'Set wb = ThisWorkbook  ' useless !
    
    Application.ScreenUpdating = False
    With Sheet8
    For Each Ws In ThisWorkbook.Worksheets
        If Ws.Name <> "Roles&Dropdowns" And Ws.Name <> .Name And Ws.Name <> "Summary" And Ws.Name <> "Consolidated Data" And Ws.Name <> "Project-SampleTemplate" Then
             lastrow1 = .Range("C" & .Rows.Count).End(xlUp)(2).Row
            .Range("C" & lastrow1).Value = Ws.Range("B1").Value
            .Range("D" & lastrow1).Value = Ws.Range("K1").Value
            .Range("E" & lastrow1).Value = Ws.Range("E1").Value
            .Range("F" & lastrow1).Value = Ws.Range("G1").Value
        End If
    Next Ws
    End With
    Application.ScreenUpdating = True
    
    End Sub

    According to efficiency in case of big data each result row should be written at once rather than cell by cell
    so the post #10 VBA procedure revamped :

    PHP Code: 
    Sub CopyPasteValuesFromAllWorkSheets2()
        
    Dim LastRow As LongWs As Worksheet
            LastRow 
    1
        With Sheet8
           
    .UsedRange.Offset(1).ClearContents
            Application
    .ScreenUpdating False
        
    For Each Ws In ThisWorkbook.Worksheets
        
    If Ws.Name <> "Roles&Dropdowns" And Ws.Name <> .Name And Ws.Name <> "Summary" And _
           Ws
    .Name <> "Consolidated Data" And Ws.Name <> "Project-SampleTemplate" Then
            LastRow 
    LastRow 1
           
    .Rows(LastRow).Columns("C:F") = Array(Ws.[B1], Ws.[K1], Ws.[E1], Ws.[G1])
        
    End If
        
    Next
        End With
            Application
    .ScreenUpdating True
    End Sub 

  14. #14
    Registered User
    Join Date
    10-03-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    98

    Re: Copy-Paste different cells from each worksheet to another worksheet (excluding some)

    Thank you Marc L and TMS very much and I really appreciate you time and efforts!!
    I will be closing this loop.

+ 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. Copy & Paste a range of cells in one worksheet to a single cell in another worksheet
    By perfinit in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-15-2022, 09:23 AM
  2. Replies: 10
    Last Post: 02-04-2021, 06:14 PM
  3. Replies: 5
    Last Post: 11-10-2014, 11:49 PM
  4. [SOLVED] Need to copy certain cells from one worksheet and paste into another worksheet
    By ddouget1 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-15-2013, 05:42 PM
  5. [SOLVED] Copy paste from various cells to one row on different worksheet
    By helpappreciated in forum Excel General
    Replies: 3
    Last Post: 12-26-2012, 02:32 AM
  6. [SOLVED] Copy data from a worksheet and paste to worksheet with a similar worksheet name
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-21-2012, 04:02 AM
  7. Copy data from a worksheet and paste to worksheet with a similar worksheet name
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-21-2012, 02:49 AM

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