I have my codes below and it is not working. After I clicked the button on sheet3 it is not working anything. I tried to add the below code in which I have search through this concern – excel VBA code not working, but still no avail. Can you help on what other things I needed to do or I may have missed? Thanks in advance!
' Call the function to copy data from Sheet1 to Sheet2
‘CopyDataToSheet2()
Sub CopyCopyPasteRowsToSheet2()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lastRow As Long
Dim i As Long
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
' Find the last row in Sheet2
lastRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row + 1
' Check conditions for each row in Sheet1 and copy & paste accordingly
For i = 2 To ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
If ws1.Cells(i, "G").Value = ws1.Cells(i, "C").Value Then
ws1.Rows(i).Range("G2:J2").Copy ws2.Cells(lastRow, "A")
lastRow = lastRow + 1
End If
If ws1.Cells(i, "K").Value = ws1.Cells(i, "C").Value Then
ws1.Rows(i).Range("K2:N2").Copy ws2.Cells(lastRow, "A")
lastRow = lastRow + 1
End If
If ws1.Cells(i, "O").Value = ws1.Cells(i, "C").Value Then
ws1.Rows(i).Range("O2:R2").Copy ws2.Cells(lastRow, "A")
lastRow = lastRow + 1
End If
If ws1.Cells(i, "S").Value = ws1.Cells(i, "C").Value Then
ws1.Rows(i).Range("S2:V2").Copy ws2.Cells(lastRow, "A")
lastRow = lastRow + 1
End If
If ws1.Cells(i, "W").Value = ws1.Cells(i, "C").Value Then
ws1.Rows(i).Range("W2:Z2").Copy ws2.Cells(lastRow, "A")
lastRow = lastRow + 1
End If
Next i
End Sub
This is what I put in that I have found on the page link I have shared above.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.EnableEvents = False
End Sub