Ninja_nutter
New Member
- Joined
- Mar 1, 2016
- Messages
- 21
- Office Version
- 365
- Platform
- Windows
Hi All,
I have 10 workbooks and a Master workbook in a folder, that I have adapted some code, to copy the data from a named sheet "Action Log" in each source wb to the master wb then loop through to the next wb.
On the master wb sheet column "A" needs to show which wb the data came from by copy/pasting the valve of cell "E1" from the source wb.
The problem I cannot solve is how to make the autofill destination range dynamic.
Any assistance with this problem will be greatly appreciated.
I have 10 workbooks and a Master workbook in a folder, that I have adapted some code, to copy the data from a named sheet "Action Log" in each source wb to the master wb then loop through to the next wb.
On the master wb sheet column "A" needs to show which wb the data came from by copy/pasting the valve of cell "E1" from the source wb.
The problem I cannot solve is how to make the autofill destination range dynamic.
Any assistance with this problem will be greatly appreciated.
VBA Code:
Sub CopyRange()
Application.ScreenUpdating = False
Dim wkbDest As Workbook
Dim wkbSource As Workbook
Dim lRow As Long
Dim last_row As Long
Dim my_Range As Range
Set wkbDest = ThisWorkbook
Const strPath As String = "H:\2021 new version\" 'Folder path for all workbooks
ChDir strPath
strExtension = Dir("*.xlsm*")
Do While strExtension <> ""
Set wkbSource = Workbooks.Open(strPath & strExtension)
With wkbSource
Sheets("Action Log").Select
Cells(Rows.Count, 2).End(xlUp).Select
lr = ActiveCell.Offset(0, 0).Select
Range(ActiveCell, "N8").Copy
.Sheets("Action Log").Range("B8:N" & Range("A" & Rows.Count).End(xlUp).Row).Copy wkbDest.Sheets("Action Log").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
.Sheets("Action Log").Range("E1").Copy 'workbook title to be copied to column A of the master workbook for each row of data copied over
wkbDest.Sheets("Action Log").Activate
lRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select
ActiveCell.PasteSpecial Paste:=xlPasteValues, operation:=xlNone, SkipBlanks:=False, Transpose:=False ' pastes the value of E1 to the first blank cell in column A
Application.CutCopyMode = False
last_row = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
Set my_Range = ActiveSheet.Range("A8:A" & last_row)
Cells(Rows.Count, 1).End(xlUp).Offset(0, 0).Select
Selection.Copy
Selection.AutoFill Destination:=Range("A8:A" & last_row) 'This is where I cannot workout how to make this range dynamic after the first wb data has been copied.
.Close savechanges:=False
End With
strExtension = Dir
Loop
Application.ScreenUpdating = True
End Sub