Macro overwrites first row instead of pasting the data on the next row.
I’m trying to get this macro to paste data into the next row but all it does is overwrite the the first row. Can anyone help me please?
Sub ValidateExportData()
‘
‘ ValidateExportData Macro
‘
‘ Keyboard Shortcut: n/a
‘
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim DataSource As Worksheet
Dim DataDestination As Worksheet
Dim LastRow As Long
Dim PasteRow As Long
On Error GoTo CleanUp
‘ Open the destination workbook
Dim TaskCalendar As Workbook
Set TaskCalendar = Workbooks.Open(“S:Task Management Calendar SystemTask Calendar.xlsx”)
‘ Set worksheets
Set DataSource = Workbooks(“Analysis Request Form.xlsm”).Worksheets(“Analysis Request Form”)
Set DataDestination = TaskCalendarWB.Worksheets(“2025”)
‘ Find the last row and move to the next row
LastRow = DataDestination.Range(“D” & Rows.Count).End(xlUp).Row + 1
‘ Copy and paste values from the source to the destination
DataDestination.Cells(PasteRow, 4).Value = DataSource.Range(“E11”).Value ‘ Paste in column D
DataDestination.Cells(PasteRow, 5).Value = DataSource.Range(“E12”).Value ‘ Paste in column E
‘ Optional: Save and close the destination workbook
TaskCalendarWB.Close SaveChanges:=True
CleanUp:
Application.CutCopyMode = False
Application.EnableEvents = True
Application.ScreenUpdating = True
If Err.Number <> 0 Then
MsgBox “An error occurred: ” & Err.Description
End If
End Sub
I’m trying to get this macro to paste data into the next row but all it does is overwrite the the first row. Can anyone help me please? Sub ValidateExportData()” ValidateExportData Macro” Keyboard Shortcut: n/a’Application.ScreenUpdating = FalseApplication.EnableEvents = FalseDim DataSource As WorksheetDim DataDestination As WorksheetDim LastRow As LongDim PasteRow As LongOn Error GoTo CleanUp’ Open the destination workbookDim TaskCalendar As WorkbookSet TaskCalendar = Workbooks.Open(“S:Task Management Calendar SystemTask Calendar.xlsx”)’ Set worksheetsSet DataSource = Workbooks(“Analysis Request Form.xlsm”).Worksheets(“Analysis Request Form”)Set DataDestination = TaskCalendarWB.Worksheets(“2025”)’ Find the last row and move to the next rowLastRow = DataDestination.Range(“D” & Rows.Count).End(xlUp).Row + 1′ Copy and paste values from the source to the destinationDataDestination.Cells(PasteRow, 4).Value = DataSource.Range(“E11”).Value ‘ Paste in column DDataDestination.Cells(PasteRow, 5).Value = DataSource.Range(“E12”).Value ‘ Paste in column E’ Optional: Save and close the destination workbookTaskCalendarWB.Close SaveChanges:=TrueCleanUp:Application.CutCopyMode = FalseApplication.EnableEvents = TrueApplication.ScreenUpdating = TrueIf Err.Number <> 0 ThenMsgBox “An error occurred: ” & Err.DescriptionEnd IfEnd Sub Read More