Tried the same. The error still persists. Attached screenshot of code.Hi,
try changing this line
VBA Code:Set wsdest1 = wb1.Sheets(shname1)
for this
VBA Code:Set wsdest1 = wb1.Worksheets(CStr(shname1))
and see if resolves your issue
Dave
Set wsdest1 = wb1.Sheets(Trim(shname1))
Sub Service_Transfer_Action_Items()
Dim wb1 As Workbook
Dim wscopy1 As Worksheet, wsdest1 As Worksheet, wsininterim1 As Worksheet
Dim shname1 As String
Dim a As Long, b As Long, lrow2 As Long, lrow3 As Long
Dim sFailed As String
Set wb1 = ActiveWorkbook
Set wscopy1 = wb1.Worksheets("SCM")
lrow2 = wscopy1.Cells(Rows.Count, "A").End(xlUp).Row
sFailed = "Sheet(s) not found: "
For a = 3 To lrow2
shname1 = wscopy1.Cells(a, 1).Value
On Error Resume Next
Set wsdest1 = wb1.Sheets(Trim(shname1))
If Err Then
sFailed = sFailed & " " & shname1
End If
On Error GoTo 0
With Application
b = .IfError(.Match(wscopy1.Range("F" & a), wsdest1.Range("D:D"), 0), 0)
End With
If b <> 0 Then
wscopy1.Range("G" & a & ":L" & a).Copy _
wsdest1.Range("E" & b & ":J" & b)
End If
Next a
If sFailed <> "Sheet(s) not found: " Then
MsgBox sFailed
Else
MsgBox "Completed"
End If
End Sub
Thanks Alex. This has helped me drill down the root cause. Error found to be with a wrong sheet number referenced.If you want to try this:
VBA Code:Sub Service_Transfer_Action_Items() Dim wb1 As Workbook Dim wscopy1 As Worksheet, wsdest1 As Worksheet, wsininterim1 As Worksheet Dim shname1 As String Dim a As Long, b As Long, lrow2 As Long, lrow3 As Long Dim sFailed As String Set wb1 = ActiveWorkbook Set wscopy1 = wb1.Worksheets("SCM") lrow2 = wscopy1.Cells(Rows.Count, "A").End(xlUp).Row sFailed = "Sheet(s) not found: " For a = 3 To lrow2 shname1 = wscopy1.Cells(a, 1).Value On Error Resume Next Set wsdest1 = wb1.Sheets(Trim(shname1)) If Err Then sFailed = sFailed & " " & shname1 End If On Error GoTo 0 With Application b = .IfError(.Match(wscopy1.Range("F" & a), wsdest1.Range("D:D"), 0), 0) End With If b <> 0 Then wscopy1.Range("G" & a & ":L" & a).Copy _ wsdest1.Range("E" & b & ":J" & b) End If Next a If sFailed <> "Sheet(s) not found: " Then MsgBox sFailed Else MsgBox "Completed" End If End Sub
Good to hear you fixed your code and it works without problems now. However, that would be great if you could post the working code to help future readers, then it is perfectly fine to mark it as the solution.I have taken cues from your code and made few changes in my code to make it work.