The problem seems to be that you are trying to set a range in one worksheet using a reference to another worksheet
Set CopyRng2 = Wkb.Sheets(2).Range(Cells(RowofCopySheet, 1), Cells(ActiveSheet.UsedRange.Rows.Count, ActiveSheet.UsedRange.Columns.Count))
In the case of the “CopyRng2” the conflict is between "Wkb.Sheets(2)" and the activesheet which in this case seems to be "shtDest" as this the one where the copypaste took place.
This is the case also in the first copy, there was not error at the first copy as the "Wkb.Sheets(1)" was also the activesheet at that time
Set CopyRng = Wkb.Sheets(1).Range(Cells(RowofCopySheet, 1), Cells(ActiveSheet.UsedRange.Rows.Count, ActiveSheet.UsedRange.Columns.Count))
To eliminate this kind of errors avoid the use of the activesheet (kind of mandatory if you are working with the multi-windows excel 2013), always be specific as to what object you are working with, using code like the following:
With WbkSrc.Worksheets(b)
Set aRngSrc(b) = Range(.Cells(kRowCopyFrom, 1), .UsedRange.SpecialCells(xlLastCell))
End With
Find below some adjustments to you original code:
I have consider the following assumptions:
The following variables are defined before this procedure
kPath
WbkTrg (target workbook)
kRowCopyFrom (RowofCopySheet)
Have also added the following constant to make it flexible the number of worksheets to be copied
Const kWshCnt As Byte = 2
Also presenting two alternatives to “paste” the values in the target worksheets (see below options 1 & 2)
Option Explicit
Option Base 1
Rem Previously defined
Const kPath As String = "D:\!EEM Documents\!Desktop\@Trash\TEST"
Const kRowCopyFrom As Byte = 6
Dim WbkTrg As Workbook
Rem New constant
Const kWshCnt As Byte = 2
Sub Solution_CopyWshsFromAllFilesInFolder()
Dim sFileSrc As String
Dim WbkSrc As Workbook
Dim aRngSrc(kWshCnt) As Range
Dim aRowIni(kWshCnt) As Long
Dim RngTrg As Range
Dim b As Byte
sFileSrc = Dir(kPath & "\*.xlsx", vbNormal)
If Len(sFileSrc) = 0 Then Exit Sub
Do Until sFileSrc = vbNullString
If Not sFileSrc = WbkTrg.Name And Not sFileSrc Like "CopyWshsFromAllFilesInFolder_*" Then
Set RngTrg = Nothing
Set WbkSrc = Workbooks.Open(Filename:=kPath & "\" & sFileSrc)
Rem Validates required number of worksheets in source workbook
If WbkSrc.Worksheets.Count >= kWshCnt Then
For b = 1 To kWshCnt
Rem Sets source range
With WbkSrc.Worksheets(b)
Set aRngSrc(b) = Range(.Cells(kRowCopyFrom, 1), .UsedRange.SpecialCells(xlLastCell))
End With
With WbkTrg.Worksheets(b)
Rem Resets the Starting row to set the values from source ranges
Rem Leaves one row between ranges to ensure no overlapping
If aRowIni(b) = 0 Then aRowIni(b) = kRowCopyFrom Else aRowIni(b) = 2 + .UsedRange.SpecialCells(xlLastCell).Row
Rem Option 1 - Brings only the values from the source ranges
Set RngTrg = Range(.Cells(aRowIni(b), 1), .Cells(-1 + aRowIni(b) + aRngSrc(b).Rows.Count, aRngSrc(b).Columns.Count))
RngTrg.Value = aRngSrc(b).Value2
Rem Option 2 - Paste the values and number formats from the source ranges
Rem This option only uses the starting cell to paste the source ranges
Set RngTrg = .Cells(aRowIni(b), 1)
aRngSrc(b).Copy
RngTrg.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.CutCopyMode = False
End With: Next: End If
WbkSrc.Close False
End If
sFileSrc = Dir()
Loop
End Sub