legalhustler
Well-known Member
- Joined
- Jun 5, 2014
- Messages
- 1,214
- Office Version
- 365
- Platform
- Windows
Hello,
The following macro copies a sheet (called "Data") from a Source workbook to a Destination workbook (both on SharePoint). When the procedure executes, the sheet is copied from the Source workbook to the Destination workbook and the Destination workbook saves & closes but then after a few second the Source workbook opens (in read only) and asks if I want to Save it after I click the X on the top right corner of the workbook. The Source workbook like the Destination workbook should NOT open after the macro finishes and I do not want any changes saved to it. I do not want the Excel application to close because I may have other workbooks open, just the two workbooks should close. Can anyone help modify the code below?
Additionally, if the macro is run more than once I would like the sheet that is copied (i.e. sheet name "Data 4.18.2024") to be replaced with the current version and renamed accordingly (i.e. sheet name "Data 4.18.2024"). I was hoping the part of the code where the comment is "Check if table with the same name exists, delete it if found" would do that but it does not. Can this also be corrected?
The following macro copies a sheet (called "Data") from a Source workbook to a Destination workbook (both on SharePoint). When the procedure executes, the sheet is copied from the Source workbook to the Destination workbook and the Destination workbook saves & closes but then after a few second the Source workbook opens (in read only) and asks if I want to Save it after I click the X on the top right corner of the workbook. The Source workbook like the Destination workbook should NOT open after the macro finishes and I do not want any changes saved to it. I do not want the Excel application to close because I may have other workbooks open, just the two workbooks should close. Can anyone help modify the code below?
Additionally, if the macro is run more than once I would like the sheet that is copied (i.e. sheet name "Data 4.18.2024") to be replaced with the current version and renamed accordingly (i.e. sheet name "Data 4.18.2024"). I was hoping the part of the code where the comment is "Check if table with the same name exists, delete it if found" would do that but it does not. Can this also be corrected?
VBA Code:
Sub PasteDataToLastWorksheet()
Dim sourceWorkbookPath As String
Dim destinationWorkbookPath As String
Dim sourceWorkbook As Workbook
Dim destinationWorkbook As Workbook
Dim sourceSheet As Worksheet
Dim destinationSheet As Worksheet
Dim currentDate As String
Dim sourceTable As ListObject
Dim destinationTable As ListObject
Dim sourceHeaderRow As Range
Dim destinationHeaderRow As Range
Dim destinationStartCell As Range
Dim sourceDataRange As Range
Dim sourceTableRange As Range
' Turn off screen updating to speed up the macro and prevent flickering
Application.ScreenUpdating = False
' Define paths for source and destination workbooks
sourceWorkbookPath = "https://.sharepoint.com/sites/DailyOrders?web=1"
destinationWorkbookPath = "https://.sharepoint.com/sites/DailyOrders.xlsm?web=1"
' Open source workbook in read-only mode
Set sourceWorkbook = Workbooks.Open(Filename:=sourceWorkbookPath, ReadOnly:=True)
Set sourceSheet = sourceWorkbook.Sheets("Data")
' Get reference to source table and its header row
Set sourceTable = sourceSheet.ListObjects("Data")
Set sourceHeaderRow = sourceTable.HeaderRowRange
' Remove filters from source table
sourceTable.AutoFilter.ShowAllData
' Get current date
currentDate = Format(Now, "m.d.yyyy")
' Open destination workbook
Workbooks.Open destinationWorkbookPath
Set destinationWorkbook = Workbooks("DailyOrders.xlsm")
' Check if table with the same name exists, delete it if found
For Each destinationSheet In destinationWorkbook.Sheets
For Each destinationTable In destinationSheet.ListObjects
If destinationTable.Name = "Data" Then
Application.DisplayAlerts = False
destinationTable.Delete
Application.DisplayAlerts = True
Exit For
End If
Next destinationTable
Next destinationSheet
' Copy source table data including header row
Set sourceTable = sourceSheet.ListObjects("Data")
Set sourceHeaderRow = sourceTable.HeaderRowRange
Set sourceDataRange = sourceTable.DataBodyRange
Set sourceTableRange = Union(sourceHeaderRow, sourceDataRange)
sourceTableRange.Copy
' Paste data into destination workbook
Dim lastSheet As Worksheet
Set lastSheet = destinationWorkbook.Sheets(destinationWorkbook.Sheets.Count)
Set destinationSheet = destinationWorkbook.Sheets.Add(After:=lastSheet)
destinationSheet.Name = "Data " & currentDate
Set destinationStartCell = destinationSheet.Range("A1")
destinationStartCell.PasteSpecial Paste:=xlPasteValues
Set destinationTable = destinationSheet.ListObjects.Add(xlSrcRange, destinationStartCell.CurrentRegion, , xlYes)
Set destinationHeaderRow = destinationTable.HeaderRowRange
destinationHeaderRow.Value = sourceHeaderRow.Value
Application.CutCopyMode = False
' Save changes in destination workbook
destinationWorkbook.Save
destinationWorkbook.Close
' Close source workbook without saving changes and hide it
sourceWorkbook.Close SaveChanges:=False
' Turn on screen updating
Application.ScreenUpdating = True
End Sub
Last edited: