+ Reply to Thread
Results 1 to 11 of 11

VBA to copy all rows in any sheet with 1 in column A and copy entire row to specific sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    03-16-2020
    Location
    Perth
    MS-Off Ver
    365
    Posts
    78

    VBA to copy all rows in any sheet with 1 in column A and copy entire row to specific sheet

    Hi,
    I have a work book with multiple tabs that depending on the project will have different names.

    I would like to summarise the entire row of any sheet/tab to a another sheet/tab called Take off.

    Because the names of the sheets can change depending on the project, I was thinking that a search of the entire note book for the "1" in column A was the best approach.


    Ultimately any row with 1 within column A would be copied into the "Take off" sheet.


    Appreciate your help.

    Lui
    Attached Files Attached Files

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    24,996

    Re: VBA to copy all rows in any sheet with 1 in column A and copy entire row to specific s

    Option Explicit
    
    Sub TakeOff()
        Dim ws As Worksheet, i As Long
        Dim sh As Worksheet, lr2 As Long
        Set sh = Sheets("Take off")
        Dim lr As Long
        Application.ScreenUpdating = False
        For Each ws In Worksheets
            If ws.Name <> "Summary" Then
                If ws.Name <> "Take off" Then
                    lr = ws.Range("C" & Rows.Count).End(xlUp).Row
                    For i = 1 To lr
                        lr2 = sh.Range("C" & Rows.Count).End(xlUp).Row
                        If ws.Range("A" & i) = 1 Then
                            ws.Range("B" & i & ":U" & i).Copy
                            sh.Range("B" & lr2 + 1).PasteSpecial xlPasteValues
                        End If
                    Next i
                End If
            End If
        Next ws
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
        MsgBox "Action Completed"
    
    End Sub
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    03-16-2020
    Location
    Perth
    MS-Off Ver
    365
    Posts
    78

    Re: VBA to copy all rows in any sheet with 1 in column A and copy entire row to specific s

    Alan,
    thank you very much. The macro does work, however if I run it again, it doesn't delete the previous results it just adds them to the bottom. Is there a away to delete the previous results when running the macro again. Please keep in mind that at the bottom of the take off page I wanted to sum the results.

    I threw some figures into the attached.

    Appreciate any further assistance.
    Attached Files Attached Files

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    24,996

    Re: VBA to copy all rows in any sheet with 1 in column A and copy entire row to specific s

    Add this line of code directly below the Application.Screenupdating = False

    sh.Range("C2").CurrentRegion.ClearContents
    See if that does what you want.

  5. #5
    Registered User
    Join Date
    03-16-2020
    Location
    Perth
    MS-Off Ver
    365
    Posts
    78

    Re: VBA to copy all rows in any sheet with 1 in column A and copy entire row to specific s

    Alan,
    again thanks.

    Don't want to be a pain, but a couple of things happening.

    In the Take off tab, when I run the macro the column heading I have along row 1 disappear. Would be great if the column headings didn't get deleted.
    Also in the take off tab, is there a way maintain a row summing the results as per my last attachment, that won't get deleted if the rows go past the formula row.

    I hope I'm making sense.

    Again thanks for your time.

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    24,996

    Re: VBA to copy all rows in any sheet with 1 in column A and copy entire row to specific s

    try this on for size:

    Option Explicit
    
    Sub TakeOff()
        Dim ws As Worksheet, i As Long
        Dim sh As Worksheet, lr2 As Long
        Set sh = Sheets("Take off")
        Dim lr As Long
        lr2 = sh.Range("C" & Rows.Count).End(xlUp).Row
        sh.Range("B2:U" & lr2).ClearContents
        Application.ScreenUpdating = False
        For Each ws In Worksheets
            If ws.Name <> "Summary" Then
                If ws.Name <> "Take off" Then
                    lr = ws.Range("C" & Rows.Count).End(xlUp).Row
                    For i = 1 To lr
                        lr2 = sh.Range("C" & Rows.Count).End(xlUp).Row
                        If ws.Range("A" & i) = 1 Then
                            ws.Range("B" & i & ":U" & i).Copy
                            sh.Range("B" & lr2 + 1).PasteSpecial xlPasteValues
                        End If
                    Next i
                End If
            End If
        Next ws
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
        MsgBox "Action Completed"
    
    End Sub

  7. #7
    Registered User
    Join Date
    03-16-2020
    Location
    Perth
    MS-Off Ver
    365
    Posts
    78

    Re: VBA to copy all rows in any sheet with 1 in column A and copy entire row to specific s

    Alan,
    unfortunately it still deletes the top row text.

    It also now keeps the formula row in place, but the formula is no longer pushed to the bottom of the list and whilst he formula text format stays, the actual formula disappears.

    Again thanks.

  8. #8
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    24,996

    Re: VBA to copy all rows in any sheet with 1 in column A and copy entire row to specific s

    Is your actual file laid out exactly the same as the file you uploaded because the code presented starts to delete at Row 2 which is the first line of data in your workbook. In the meantime, change this line to fix the final formula and keep it in place.

    sh.Range("B2:U" & lr2).ClearContents
    to
    sh.Range("B2:U" & lr2-1).ClearContents

  9. #9
    Registered User
    Join Date
    03-16-2020
    Location
    Perth
    MS-Off Ver
    365
    Posts
    78

    Re: VBA to copy all rows in any sheet with 1 in column A and copy entire row to specific s

    Alan,
    got a run time error "1004".


    I have attached the file again.

    Thanks.
    Attached Files Attached Files

  10. #10
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    24,996

    Re: VBA to copy all rows in any sheet with 1 in column A and copy entire row to specific s

    I have attached your file that runs and keeps the formula in line 48. Code is embedded in the file.
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    03-16-2020
    Location
    Perth
    MS-Off Ver
    365
    Posts
    78

    Re: VBA to copy all rows in any sheet with 1 in column A and copy entire row to specific s

    Alan,
    just getting back to this now.

    if there are more that 48 lines being copied then the formula on row 48 is overridden.

    Can the formula just be added to one row after the last row, wherever the last row ends up?

+ 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. Find specific word in Column A & copy that row & the next 30 rows to a new sheet
    By ludsonline in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-08-2017, 08:04 AM
  2. Replies: 6
    Last Post: 11-13-2015, 07:36 AM
  3. Auto copy rows to different sheet when specific column is filled
    By AgustSig in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-20-2014, 10:34 AM
  4. [SOLVED] Copy rows with specific text in specific column into specific sheet
    By Valemaar in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 08-22-2014, 03:23 PM
  5. Search string in a column then copy entire rows to another sheet
    By vannylette in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-12-2013, 12:16 PM
  6. Copy rows with values in specific column to first empty row in specified sheet
    By scaffdog845 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-20-2013, 08:29 PM
  7. Replies: 0
    Last Post: 02-06-2009, 02:45 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