Autofill last row error

marlline

New Member
Joined
Jun 22, 2017
Messages
10
Hi guys,

I know there are many posts on this topic. However I still could not figure out for my specific case. Please help. Thank you!
It errors at
Selection.AutoFill Destination:=ThisWorkbook.Sheets("Output").Range("A" & targetrow & ":C" & LastRow), Type:=xlFillCopy

The full code is as follows:
Sub DoFolder(Folder)
Dim SubFolder
Dim WKB_SOURCE As Workbook
Dim LastRow As Integer
Dim targetrow As Integer




For Each SubFolder In Folder.SubFolders
DoFolder SubFolder
Next
Dim File
For Each File In Folder.Files
targetrow = ThisWorkbook.Sheets("Output").Range("D20000").End(xlUp).Row + 1
Set WKB_SOURCE = Workbooks.Open(File, False)
WKB_SOURCE.Sheets(1).Unprotect ("BD MAP")
LastRow = WKB_SOURCE.Sheets(1).Range("C2000").End(xlUp).Row

WKB_SOURCE.Sheets(1).Range("C10:K" & LastRow).Copy
ThisWorkbook.Sheets("Output").Activate
ThisWorkbook.Sheets("Output").Range("D" & targetrow).PasteSpecial xlPasteValues
ThisWorkbook.Sheets("Output").Range("D" & targetrow).PasteSpecial xlPasteFormats

WKB_SOURCE.Sheets(1).Range("D2:D4").Copy
ThisWorkbook.Sheets("Output").Activate
ThisWorkbook.Sheets("Output").Range("A" & targetrow).PasteSpecial xlPasteValues, Transpose:=True
LastRow = ThisWorkbook.Sheets("Output").Range("D20000").End(xlUp).Row
ThisWorkbook.Sheets("output").Activate
ThisWorkbook.Sheets("Output").Range("A" & targetrow & ":C" & targetrow).Select
Selection.AutoFill Destination:=ThisWorkbook.Sheets("Output").Range("A" & targetrow & ":C" & LastRow), Type:=xlFillCopy





WKB_SOURCE.Sheets(1).Protect ("BD MAP")
WKB_SOURCE.Close savechages

Next
LastRow = ThisWorkbook.Sheets("Output").Range("D50000").End(xlUp).Row
ThisWorkbook.Sheets("Output").Range("D2").Copy
ThisWorkbook.Sheets("Output").Range("A2:C" & LastRow).PasteSpecial xlPasteFormats

End Sub
 
Re: Autofill last row error Please help!

What are the contents of the cells copied in this statement :-
Code:
WKB_SOURCE.Sheets(1).Range("D2:D4").Copy
Are they formulae?
What is the formatting for each cell?
Are the cells merged?
Can we assume that LastRow is 1143?
Or is it 1142 because when this statement
Code:
WKB_SOURCE.Sheets(1).Range("C10:K" & LastRow).Copy
was executed the value of LastRow was 10?

If you have only copied one row to the Output sheet then the statement that is causing the error doesn't need to be executed.

hth
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,223,958
Messages
6,175,632
Members
452,661
Latest member
Nonhle

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top