Consulting

Results 1 to 6 of 6

Thread: Copying to another sheet.

  1. #1

    Copying to another sheet.

    Need help on this please:


    • In a workbook, starting on sheet 5, going to the last sheet in the workbook (it can be any number of sheets).

      Starting on row 4, if there is a value in Column T, U or V then copy the entire row to a new sheet at the end of the workbook (starting on row 4). And have that sheet called, "Totals".

      Thank you.




  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Try

    Sub copy_data_from_sheets_5_to_n()
    
    
        Dim ws As Worksheet
        Dim i As Long, j As Long, LastRow As Long
        
        With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
        End With
        
        On Error Resume Next
        For Each ws In Worksheets
            If UCase(ws.Name) = "TOTALS" Then ws.Delete
        Next ws
        
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Totals"
        
        For i = 5 To Worksheets.Count - 1
            With Worksheets(i)
                LastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
                For j = 4 To LastRow
                    If Application.CountA(.Range("T" & j & ":V" & j)) > 0 Then
                        .Rows(j).Copy Worksheets("Totals").Range("A" & Rows.Count).End(xlUp).Offset(1)
                    End If
                Next j
            End With
        Next i
    
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Doesn't work correctly. I have added a file showing the final output how it should look. Notice there are hidden columns.

    sample714.xls

    Thanks for the attempt.

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    actually it will work for the requirement in your first message. with well designed worksheets, of course.


    the workbook you posted tells me you want to copy columns A-V to totals.


    Sub copy_data_from_sheets_5_to_n()
         
        Dim ws As Worksheet
        Dim i As Long, j As Long, LastRow As Long, LRTotals As Long
         
        With Application
            .DisplayAlerts = False
            .ScreenUpdating = False
        End With
         
        On Error Resume Next
        For Each ws In Worksheets
            If UCase(ws.Name) = "TOTALS" Then ws.Delete
        Next ws
         
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Totals"
        LRTotals = 4
        
        For i = 5 To Worksheets.Count - 1
            With Worksheets(i)
                LastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
                For j = 4 To LastRow
                    If Application.CountA(.Range("T" & j & ":V" & j)) > 0 Then
                        .Range("A" & j & ":V" & j).Copy Worksheets("Totals").Range("A" & LRTotals)
                        LRTotals = Worksheets("Totals").Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1
                    End If
                Next j
            End With
        Next i
         
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    That worked perfectly. Thank you.

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •