Hello everyone, I am attempting to use the macro below, but for some reason the last section of the code (specific line item bolded and underlined) results in an error.
"Run-time error 1004: method range of object worksheet failed"
Any thoughts on why this is happening and how I can fix this?
"Run-time error 1004: method range of object worksheet failed"
Any thoughts on why this is happening and how I can fix this?
Code:
Sub Res_Hrs_Cost()
Dim wb As ThisWorkbook
Dim Sh As Worksheet
Dim CopyRng As Range
Dim Pricing As Worksheet
Dim BaseDate As Range
Dim BaseDate_Full As Range
Dim Month_2_Full As Range
Dim Heading_Month_1 As Range
Dim Num_Months As Integer
Dim Dest_Sh As Worksheet
Dim Dest_Start_Row As Integer
Dim Dest_End_Row As Integer
Dim Dest_End_Column As Integer
Dim Dest_Start_Row_2 As Integer
Dim Dest_End_Row_2 As Integer
Dim Source_Sh As Worksheet
Dim Source_Start_Row As Integer
Dim Source_End_Row As Integer
With Application
.DisplayAlerts = False
.ScreenUpdating = False
.EnableEvents = False
End With
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '(1.) Clear contents from destination worksheet
Set Dest_Sh = Sheets("DESTINATION")
Dest_End_Row = Dest_Sh.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
Dest_End_Column = Dest_Sh.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
On Error Resume Next
Dest_Sh.Visible = True
Dest_Sh.Activate
On Error GoTo 0
If Dest_End_Row > 1 Then
Dest_Sh.Rows("2:" & Dest_End_Row).EntireRow.Delete
End If
If Dest_End_Column > 50 Then
Dest_Sh.Range(Cells(1, 9), Cells(1, Dest_End_Column)).EntireColumn.Delete
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'(2.) Copy source data and paste to destination worksheet
Set Source_Sh = Sheets("SOURCE")
Source_End_Row = Source_Sh.Range("K" & Rows.Count).End(xlUp).Row
On Error Resume Next
Dest_Sh.Visible = True
Dest_Sh.Activate
On Error GoTo 0
'COPY RANGE - 1
Dest_Start_Row = Dest_Sh.Range("A" & Rows.Count).End(xlUp).Row + 1
Set CopyRng = Source_Sh.Range("B14", "B" & Source_End_Row)
CopyRng.Copy
With Dest_Sh.Range("A" & Dest_Start_Row)
.PasteSpecial 8 ' Column width
.PasteSpecial xlPasteValues
Application.CutCopyMode = False
End With
'COPY RANGE - 2
Dest_End_Row_2 = Dest_Sh.Cells(Rows.Count, "A").End(xlUp).Row
Set CopyRng = Source_Sh.Range("K14", "K" & Source_End_Row)
CopyRng.Copy
With Dest_Sh.Range("B" & Dest_Start_Row)
.PasteSpecial 8 ' Column width
.PasteSpecial xlPasteValues
Application.CutCopyMode = False
End With
'COPY RANGE - 3
Set CopyRng = Source_Sh.Range("AA14", "AA" & Source_End_Row)
CopyRng.Copy
With Dest_Sh.Range("C" & Dest_Start_Row)
.PasteSpecial 8 ' Column width
.PasteSpecial xlPasteValues
Application.CutCopyMode = False
End With
'INSERT - 1
If Dest_End_Row > 1 Then
Dest_Sh.Range(Cells(Dest_Start_Row, 7), Cells(Dest_End_Row_2, 7)).Formula = "D"
End If
'COPY RANGE - 4
Set Pricing = Sheets("Pricing")
Pricing.Visible = True
Set BaseDate = Pricing.Range("$I$16")
BaseDate.Copy
With Dest_Sh.Range("H" & Dest_Start_Row, "H" & Dest_End_Row_2)
.PasteSpecial xlPasteValues
Application.CutCopyMode = False
End With
Set BaseDate_Full = Pricing.Range("$I$14")
BaseDate_Full.Copy
With Dest_Sh.Range("$I$1")
.PasteSpecial xlPasteValues
Application.CutCopyMode = False
End With
'MONTH HEADINGS!
On Error Resume Next
Dest_Sh.Visible = True
Dest_Sh.Activate
On Error GoTo 0
Set Heading_Month_1 = Dest_Sh.Range("$I$1")
Num_Months = Pricing.Range("$I$19")
If Heading_Month_1 > 0 Then
Dest_Sh.Range(Cells(1, 10), Cells(1, 8 + Num_Months)).Formula = "=DATE(YEAR(I$1),MONTH(I$1)+1,DAY(I$1))"
End If
'COPY RANGE - 5
[I][U][B] Set CopyRng = Source_Sh.Range(Cells(14, 50), Cells(Source_End_Row, 49 + Num_Months))[/B][/U][/I]
CopyRng.Copy
With Dest_Sh.Range(Cells(Dest_Start_Row, 9), Cells(Dest_End_Row_2, 8 + Num_Months))
.PasteSpecial 8 ' Column width
.PasteSpecial xlPasteValues
Application.CutCopyMode = False
End With
ExitTheSub:
Application.GoTo Dest_Sh.Cells(1)
ActiveWindow.DisplayGridlines = False
With Application
.DisplayAlerts = True
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub