Help with Extract of data from Mutiple files

hussey1213

New Member
Joined
Aug 15, 2007
Messages
14
I'm having trouble with the range porition of below. Right now it pulls in B2:B10 but not all files this is pulling from will have the same amount of data to copy. I've tried using B2:B & LastRow but I'm not having luck.


Sub x()

Dim nCount As Long, wbResults As Workbook, rPaste As Range, wsTo As Worksheet, wsFrom As Worksheet, sFolder As String, wsData As Worksheet


With Application.FileDialog(msoFileDialogFolderPicker)
.Show
On Error GoTo errline
sFolder = .SelectedItems(1)
End With

Set wsTo = ThisWorkbook.Sheets("Sheet1")
wsTo.Activate
Set rPaste = Application.InputBox("Enter starting cell to paste", Type:=8)
If rPaste Is Nothing Or rPaste.Count > 1 Then Exit Sub

With Application
.ScreenUpdating = False: .DisplayAlerts = False: .EnableEvents = False
End With
On Error Resume Next

With Application.FileSearch
.NewSearch
.LookIn = sFolder
.FileType = msoFileTypeExcelWorkbooks
If .Execute > 0 Then
For nCount = 1 To .FoundFiles.Count
Set wbResults = Workbooks.Open(Filename:=.FoundFiles(nCount), UpdateLinks:=0)
For Each wsFrom In wbResults.Worksheets

With wsTo.Range(rPaste.Address)
.Offset(1, 0).Resize(21).Value = wsFrom.Range("B2:B10").Value
End With
Set rPaste = rPaste.Offset(20)
Next wsFrom
wbResults.Close SaveChanges:=True
Next nCount
End If
End With

On Error GoTo 0
With Application
.ScreenUpdating = False: .DisplayAlerts = True: .EnableEvents = True
End With

errline:

End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

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