I would like to write a VBA code that does the following: Open in the background, the…
- User
I would like to write a VBA code that does the following: Open in the background, the workbook 'G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Service Providers History.xlsm', For the Active Sheet in workbook Service Providers, Search column I from row 6 downwards to the first empty cell. Copy the rows from column B6 to the last row in L where the next empty cell in column I is empty. Paste this range into the next empty row in the same columns of the identical sheet name in the workbook 'Service Providers History.xlsm' Save and close 'Service Providers History.xlsm' Clear the contents from column B6 to the last row in column L where the next empty cell in column I is empty. Move the rows (B:L) below the recently cleared contents up to row 6. In Cell C5, D5, E5, F5, G5, H5, I5, insert the value of todays date. Pop up message "Completed Tasks have been backedup to History" Enable all Events
- Assistant
Sure! Here's the VBA code that accomplishes the tasks you described:
Sub CopyToHistory() Dim sourceWB As Workbook Dim sourceWS As Worksheet Dim historyWB As Workbook Dim historyWS As Worksheet Dim lastRow As Long Dim pasteRow As Long ' Open the source workbook in the background Set sourceWB = Workbooks.Open("G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Service Providers.xlsm") Set sourceWS = sourceWB.ActiveSheet ' Find the last row in column I lastRow = sourceWS.Cells(sourceWS.Rows.Count, "I").End(xlUp).Row ' Copy the range from B6 to the last row in L sourceWS.Range("B6:L" & lastRow).Copy ' Open the history workbook and select the corresponding sheet Set historyWB = Workbooks.Open("G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\Service Providers History.xlsm") Set historyWS = historyWB.Sheets(sourceWS.Name) ' Find the next empty row in the history sheet pasteRow = historyWS.Cells(historyWS.Rows.Count, "B").End(xlUp).Row + 1 ' Paste the copied range into the history sheet historyWS.Range("B" & pasteRow & ":L" & (pasteRow + lastRow - 6)).PasteSpecial xlPasteValues ' Save and close the history workbook historyWB.Save historyWB.Close ' Clear the contents from B6 to the last row in L sourceWS.Range("B6:L" & lastRow).ClearContents ' Move the rows below the cleared contents up to row 6 sourceWS.Range("B7:L" & lastRow).Cut sourceWS.Range("B6").Insert Shift:=xlDown ' Insert today's date in C5 to I5 Dim currentDate As Date currentDate = Date sourceWS.Range("C5:I5").Value = currentDate ' Display a message box MsgBox "Completed tasks have been backup to History" ' Enable all events Application.EnableEvents = True End Sub - User
When the workbook 'Service Providers History' is opened, select the sheet with the same name as the active sheet in workbook 'Service Providers' and the paste into that sheet in workbook 'Service Providers History'
- Assistant
- User
If a sheet with the same name does not exist in the “Service Providers History.xlsm” workbook, can you create a new sheet with the same name then copy the entire sheet (values, formats, and formulas) from workbook "Service Providers" and paste into it.
- Assistant
- User
Can the VBA codes of the sheet also be copied across? If not then can a message be popped up saying "Contractor Sheet was not found in History but has been created. Remember to manually copy required VBA codes across"
- Assistant
- User
The source workbook 'Service Providers' sourceWB will already be open and Active. Do I have to open it again?
- Assistant
- User
PasteSpecial method of Range class failed on this line of the code; historyWS.Range("B" & pasteRow & ":L" & (pasteRow + lastRow - 6)).PasteSpecial xlPasteValues
- Assistant
Apologies for the oversight. The
PasteSpecialmethod cannot be used directly on a range in another workbook. Instead, you can assign the values from the copied range to the destination range in the history worksheet. Here's the updated code that replaces thePasteSpecialline:' Paste the copied range into the history sheet historyWS.Range("B" & pasteRow).Resize(lastRow - 5, 12).Value = sourceWS.Range("B6:L" & lastRow).ValueThis code uses the
Valueproperty to assign the values directly from the source range to the destination range in the history worksheet. TheResizemethod is used to match the size of the copied range.With this modification, the code should now correctly copy the values from the source range to the destination range in the "Service