I am writing an Excel macro (Excel 2016) to copy data between sheets. Rather than use the typical Range command (for instance, Sheet2.Range("A1:A15").Value = Sheet1.Range("A1:A15").Value) I want to use Named Ranges for the Columns, in case I ever insert columns in either sheet in the future. I also want to use Intersect, in case I ever add/delete rows in the future. I have been able to get this to work when I am copying data within the SAME worksheet, but it is giving me a "400 error" when I try to do it from one worksheet to another. I am not sure what I am doing wrong.
First, here is a line of code that works when copying data within the same worksheet.
Intersect([P1B_CalcPercent1], Rows(FirstRow & ":" & LastRow)).Value = Intersect([P1B_CalcPercent2], Rows(FirstRow & ":" & LastRow)).Value
Here is my sub for trying to copy a range of cells from one sheet to another. The line that begins with Intersect is giving me the 400 error.
Sub Copy_from_1A_Button()
Dim FirstRow As Integer, LastRow As Integer
Application.ScreenUpdating = False
ThisWorkbook.Sheets("Page 1B").Activate
Module7.UnprotectPage1BSheet
FirstRow = Range("B_first_row").Value
LastRow = Range("B_last_row").Value
If MsgBox("Are you sure you wish to do this? Data in the target cells will be overwritten.", vbYesNo) = vbYes Then
Intersect([P1B_JobTitles], Rows(FirstRow & ":" & LastRow)).Value = Intersect([P1A_JobTitles], Rows(FirstRow & ":" & LastRow)).Value
End If
Module7.ProtectPage1BSheet
Application.ScreenUpdating = True
End Sub
Both 'P1A_JobTitles' and 'P1B_JobTitles' are named ranges that have a scope of Workbook, but they exist on different sheets. (Side question: would it matter if the scope of each was just the sheet it was on?)
I tried putting "Application." in front of Intersect in both places, but I got the same error.
Any ideas why the Intersect method isn't working to copy from one sheet to another?