Code is writing over instead of appending

Brutusar

Board Regular
Joined
Nov 23, 2019
Messages
166
Office Version
  1. 365
Platform
  1. Windows
Hi, I have a workbook with a number of sheets where many rows has no value in column A, but have values in other columns. What the code needs to do is to do is this:

  • Loop thru all rows in all sheets
  • If a row has no value in column A, copy and append the row to a sheet called "Note", starting in cell A1
I have written a code meant to do this but in stead of appending the rows, the values are being overwritten in the same cells all the time, so no appending is going on.

My code is below, can somebody have a look and maybe point me in the right direction of what I have done wrong?

VBA Code:
Sub CopyEmptyRowsToNoteSheet()

    Dim ws As Worksheet
    Dim noteSheet As Worksheet
    Dim lastRow As Long
    Dim i As Long
    
    Set noteSheet = ThisWorkbook.Sheets("Note")
    
    For Each ws In ThisWorkbook.Sheets
        If ws.Name <> "Note" Then
            lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
            
            For i = 1 To lastRow
                If ws.Cells(i, "A").Value = "" Then
                    ws.Cells(i, "A").EntireRow.Copy noteSheet.Cells(noteSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1, "A")
                End If
            Next i
        End If
    Next ws
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I think you may need to update this line:
Rich (BB code):
ws.Cells(i, "A").EntireRow.Copy noteSheet.Cells(noteSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1, "A")
to this:
Rich (BB code):
ws.Cells(i, "A").EntireRow.Copy noteSheet.Cells(noteSheet.Cells(noteSheet.Rows.Count, "A").End(xlUp).Row + 1, "A")
 
Upvote 0
Try:
VBA Code:
Sub CopyEmptyRowsToNoteSheet()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, noteSheet As Worksheet, lastRow As Long, i As Long, x As Long: x = 1
    Set noteSheet = ThisWorkbook.Sheets("Note")
    For Each ws In ThisWorkbook.Sheets
        If ws.Name <> "Note" Then
            lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
            For i = 1 To lastRow
                If ws.Cells(i, "A").Value = "" Then
                    ws.Cells(i, "A").EntireRow.Copy noteSheet.Range("A" & x)
                    x = x + 1
                End If
            Next i
        End If
    Next ws
    Application.ScreenUpdating = False
End Sub
 
Upvote 0
I think you may need to update this line:
Rich (BB code):
ws.Cells(i, "A").EntireRow.Copy noteSheet.Cells(noteSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1, "A")
to this:
Rich (BB code):
ws.Cells(i, "A").EntireRow.Copy noteSheet.Cells(noteSheet.Cells(noteSheet.Rows.Count, "A").End(xlUp).Row + 1, "A")
Great, thanks!
 
Upvote 0
Try:
VBA Code:
Sub CopyEmptyRowsToNoteSheet()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, noteSheet As Worksheet, lastRow As Long, i As Long, x As Long: x = 1
    Set noteSheet = ThisWorkbook.Sheets("Note")
    For Each ws In ThisWorkbook.Sheets
        If ws.Name <> "Note" Then
            lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
            For i = 1 To lastRow
                If ws.Cells(i, "A").Value = "" Then
                    ws.Cells(i, "A").EntireRow.Copy noteSheet.Range("A" & x)
                    x = x + 1
                End If
            Next i
        End If
    Next ws
    Application.ScreenUpdating = False
End Sub
Working perfect, thanks!
 
Upvote 0
You are very welcome. :)
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top