Search code examples
vbaloopsif-statementnested-loopsworksheet

VBA Copy non-blank cells in EACH worksheet to existing worksheet


I don't even know where to start so I don't have any example code. I am thinking that I need a nested loop but that is what throws me off. I look forward to learning from everyone.

Here is what I'd like to do:

  1. Begin with the worksheet named "John" and loop through each worksheet to the right.
  2. On each worksheet, if a cell in column L is not blank, copy cell F and cell L for that row.
  3. Append all of the copied cells to the worksheet "Notes". Paste the data from F on each sheet to column A and paste the corresponding data from L in column B. Add the copied data from each worksheet to the end of the data in "Notes".

I really appreciate any help, thanks!!

UPDATE Based on Alter's great help and suggestions, this is what I have and it works perfectly. Thanks Alter!

    Sub test()
        Dim ws As Worksheet
        Dim notes_ws As Worksheet
        Dim row
        Dim lastrow
        Dim notes_nextrow

        'find the worksheet called notes
        For Each ws In Worksheets
            If ws.Name = "Notes" Then
                Set notes_ws = ws
            End If
        Next ws

        'get the nextrow to print to
        notes_nextrow = notes_ws.Range("A" & Rows.Count).End(xlUp).row + 1

        'loop through other worksheets
        For Each ws In Worksheets
            'ignore the notes worksheet
            If ws.Name <> "Notes" And ws.Index > Sheets("John").Index Then
                'find lastrow
                lastrow = ws.Range("L" & Rows.Count).End(xlUp).row
                For row = 1 To lastrow
                    'if the cell is not empty
                    If IsEmpty(ws.Range("L" & row)) = False Then
                        notes_ws.Range("A" & notes_nextrow).Value = ws.Range("F" & row).Value
                        notes_ws.Range("B" & notes_nextrow).Value = ws.Range("L" & row).Value
                        notes_nextrow = notes_nextrow + 1
                    End If
                Next row
            End If
        Next ws
    End Sub

Solution

  • Nested loop indeed, you can use the code below as a basis for what you want to do

    Public Sub test()
        Dim ws As Worksheet
        Dim notes_ws As Worksheet
        Dim row
        Dim lastrow
        Dim notes_nextrow
    
        'find the worksheet called notes
        For Each ws In Worksheets
            If ws.name = "Notes" Then
                Set notes_ws = ws
            End If
        Next ws
    
        'get the nextrow to print to
        notes_nextrow = notes_ws.Range("A" & Rows.Count).End(xlUp).row + 1
    
        'loop through other worksheets
        For Each ws In Worksheets
            'ignore the notes worksheet
            If ws.name <> "Notes" Then
                'find lastrow
                lastrow = ws.Range("L" & Rows.Count).End(xlUp).row
                For row = 1 To lastrow
                    'if the cell is not empty
                    If IsEmpty(ws.Range("L" & row)) = False Then
                        notes_ws.Range("A" & notes_nextrow).Value = ws.Range("L" & row).Value
                        notes_nextrow = notes_nextrow + 1
                    End If
                Next row
            End If
        Next ws
    End Sub