VBA Code to pull first 5 lines from only the first file in a directory

Kmac224

New Member
Joined
Jun 15, 2015
Messages
47
Office Version
  1. 2010
Platform
  1. Windows
Hello,

I have directory with multiple excel files and I need to pull the first 5 lines of only the first file in the directory into a new sheet. Can anyone help?

Thanks in advance!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi Kmac224,

Hope this helps

Option Explicit
Sub GetFilesInFolder()
Dim strFolder As String 'folder to be searched eg: c:\main
Dim lngFindHowManyFiles As Long 'no of files to be returned

strFolder = "c:\main"
lngFindHowManyFiles = 5

FilesFoundNewFolder strFolder, lngFindHowManyFiles




End Sub
Public Function FilesFoundNewFolder(strPath As String, lngNoFiles As Long)


Dim vaArray As Variant
Dim i As Integer
Dim ws As Excel.Worksheet
Dim oFile As Object
Dim oFSO As Object
Dim oFolder As Object
Dim oFiles As Object
Dim lngFilesFound As Long

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(strPath)
Set oFiles = oFolder.Files


If oFiles.Count = 0 Then Exit Function 'exit function no files found



ReDim vaArray(1 To oFiles.Count)
Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))

If oFiles.Count > lngNoFiles Then
ws.Name = "First " & lngNoFiles & " Found"
Else
ws.Name = "Only " & oFiles.Count & " Found"
End If
ws.Range("A1").Value = "No"
ws.Range("B1").Value = "File Name"
ws.Range("C1").Value = "Found in Folder"
ws.Range("D1").Value = strPath
i = 1
For Each oFile In oFiles
ws.Range("A" & (i + 1)).Value = i
ws.Range("B" & (i + 1)).Value = oFile.Name


i = i + 1
If i > lngNoFiles Then Exit For 'only return required no of files
Next
ws.Cells.Columns.AutoFit
Erase vaArray
Set ws = Nothing
Set oFiles = Nothing
Set oFolder = Nothing
Set oFile = Nothing
Set oFSO = Nothing
End Function


Regards
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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