Альтернативный метод с использованием оператора Like
для проверки на книги источника / назначения. Также предоставляет способ определения диапазонов источника / назначения, которые могут быть циклически повторены для упрощения отладки и обновления позже. Код сильно комментируется для ясности.
Sub tgr()
Dim wb As Workbook
Dim wsSource As Worksheet
Dim wsDest As Worksheet
'Check if exactly 2 workbooks are currently open
If Application.Workbooks.Count <> 2 Then
MsgBox "ERROR - There are [" & Application.Workbooks.Count & "] workbooks open." & Chr(10) & _
"There must be two workbooks open:" & Chr(10) & _
"-The source workbook (old template)" & Chr(10) & _
"-The destination workbook"
Exit Sub
End If
For Each wb In Application.Workbooks
If wb.Name Like "*#.xls?" Then
'Workbook name ends in number(s), this is the source workbook that will be copied from
'You'll need to specify which sheet you're working with, this example code assumes the activesheet of that workbook
Set wsSource = wb.ActiveSheet
Else
'Workbook name does not end in number(s), this is the source workbook that will be pasted to
'You'll need to specify which sheet you're working with, this example code assumes the activesheet of that workbook
Set wsDest = wb.ActiveSheet
End If
Next wb
'Check if both a source and destination were assigned
If wsSource Is Nothing Then
MsgBox "ERROR - Unable to find valid source workbook to copy data from"
Exit Sub
ElseIf wsDest Is Nothing Then
MsgBox "ERROR - Unable to find valid destination workbook to paste data into"
Exit Sub
End If
'The first dimension is for how many times you need to define source and dest ranges, the second dimension should always be 1 to 2
Dim aFromTo(1 To 2, 1 To 2) As Range
'Add source copy ranges here: 'Add destination paste ranges here
Set aFromTo(1, 1) = wsSource.Range("M7:R19"): Set aFromTo(1, 2) = wsDest.Range("M7")
Set aFromTo(2, 1) = wsSource.Range("S7:AT16"): Set aFromTo(2, 2) = wsDest.Range("U7")
'Set aFromTo(3, 1) = wsSource.Range("M21:R33"): Set aFromTo(3, 2) = wsDest.Range("M21") 'Example of a third copy/paste range - Dim aFromTo(1 to 3, 1 to 2)
'Set aFromTo(4, 1) = wsSource.Range("S21:AT30"): Set aFromTo(4, 2) = wsDest.Range("U21") 'Example of a fourth copy/paste range - Dim aFromTo(1 to 4, 1 to 2)
'This will loop through the array of specified FromTo ranges and will ensure that only values are brought over
Dim i As Long
For i = LBound(aFromTo, 1) To UBound(aFromTo, 1)
aFromTo(i, 2).Resize(aFromTo(i, 1).Rows.Count, aFromTo(i, 1).Columns.Count).Value = aFromTo(i, 1).Value
Next i
End Sub