I would like to create VBA code for a command button that does the following: 1. Loops through all worksheets except master worksheet (SOLD) 2. Copies rows if condition is met (Paid) 3. Pastes rows to Master worksheet (Sold)
So far I have the code but my problem is that it loops through all worksheets including master worksheet (i.e duplicates information in master worksheet) and if I try to write for If ws.name<>"Sold" then The code stops working giving message box error . Please help me modify the code.
Private Sub CommandButton1_Click()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Dim i, LastRow
LastRow = ws.Range("A" & Rows.count).End(xlUp).Row
For i = 2 To LastRow
If ws.Cells(i, "M").Value = "Paid" Then
ws.Cells(i, "M").EntireRow.Copy Destination:=Sheets("Sold").Range("A" & Rows.count).End(xlUp).Offset(1)
End If
Next i
Next ws
End Sub
I don't know where you put the IF statement, but it must work in this way:
Dim ws As Worksheet
Dim i, LastRow
For Each ws In Worksheets
If ws.Name <> "Sold" Then
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If ws.Cells(i, "M").Value = "Paid" Then
ws.Cells(i, "M").EntireRow.Copy Destination:=Sheets("Sold").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
End If
Next ws