My code should copy data from excel file TimeSheet. Each file contains header with person name, and blocks with daily records. Code is looping all excel file in folder and appending records to master file. Code was partly solved here. But now I have problem with filling out column A with Person name.
Code should first fill column B in NewSht with tasks and later grab value from filed B7 OldSht and copy it to first empty cell in column A NewSht and fill until last Row in column B. With current code I get
'Autofill method of Range class failed'
error.
Sub AllFiles()
Application.EnableCancelKey = xlDisabled
Dim folderPath As String
Dim Filename As String
Dim wb As Workbook
Dim Masterwb As Workbook
Dim sh As Worksheet
Dim NewSht As Worksheet
Dim OldSht As Worksheet
Dim FindRng As Range
Dim target As Range
Dim LastRow As Long
' set master workbook
Set Masterwb = Workbooks("result2.xlsm")
folderPath = "C:\Users\TimeSheets2019\inside" 'contains folder path
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
Application.ScreenUpdating = False
Filename = Dir(folderPath & "*.xlsx*")
Do While Filename <> ""
Set wb = Workbooks.Open(folderPath & Filename, ReadOnly:=True)
Set NewSht = Masterwb.Worksheets("Tabelle1") ' added
Set OldSht = wb.Worksheets("Manager Form") ' added
' get the first empty row in the new sheet
Set FindRng = NewSht.Cells.Find(What:="*", Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
Set target = NewSht.Cells(NewSht.Rows.Count, 2).End(xlUp).Offset(1, 0)
LastRow = NewSht.Range("B" & Rows.Count).End(xlUp).Row
'copy taks list to column B
OldSht.Range("B7:B15, B17:B26").Copy
NewSht.Cells(NewSht.Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
' copy person name until last row in coulmn B
OldSht.Range("B5").AutoFill Destination:=NewSht.Range("A2:A" & LastRow), Type:=xlFillDefault
wb.Close False
Exit_Loop:
Set wb = Nothing
Filename = Dir
Loop
Application.ScreenUpdating = True
End Sub
As already pointed out in the comments, Range.AutoFill
only works if the destination range includes the source range. You can't autofill from one sheet to another.
I think you just need a simple value transfer here:
NewSht.Range("A2:A" & LastRow).Value = OldSht.Range("B5").Value
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.