-2

I had previously asked this question in this post: Importing multiple text files using VBA Macro

After a while, i created an entire new workbook and when i try to copy/paste this code to a macro module, i get a compile error: user-defined type not defined.... The code is as following:

Sub ReadFilesIntoActiveSheet()
Dim fso As FileSystemObject
Dim folder As folder
Dim file As file
Dim FileText As TextStream
Dim TextLine As String
Dim Items() As String
Dim i As Long
Dim cl As Range
Dim sFolder As String, vDB, Ws As Worksheet
Dim rngT As Range
' Get a FileSystem object
  Set fso = New FileSystemObject

' get the directory you want

sFolder = "C:\Users\danial.smith\Documents\Maintenance\DataDump\Reports\RawData\"
Set folder = fso.GetFolder(sFolder)
' set the starting point to write the data to
Set Ws = ActiveSheet
'Set cl = ActiveSheet.Cells(1, 1)

' Loop thru all files in the folder
For Each file In folder.Files
    Workbooks.Open Filename:=sFolder & file.Name, Format:=1
    With ActiveWorkbook.ActiveSheet
        vDB = .UsedRange.Offset(1)
    End With
    ActiveWorkbook.Close
    Set rngT = Ws.Range("a" & Rows.Count).End(xlUp)(2)
    rngT.Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB
Next file
Set FileText = Nothing
Set file = Nothing
Set folder = Nothing
Set fso = Nothing

End Sub

I have tried to include the Microsoft ActiveX Data Object Library as some suggest but still no luck....not sure what to do...

Mesut Akcan
  • 899
  • 7
  • 19
Dani
  • 1
  • 1
  • 3
  • Pro-tip, the VBE highlights the type that is missing... – Comintern Nov 28 '18 at 17:28
  • [Using early binding and late binding in Automation](https://support.microsoft.com/en-us/help/245115/using-early-binding-and-late-binding-in-automation) | https://stackoverflow.com/questions/49789611/vba-early-vs-late-binding-real-life-performance-differences | https://stackoverflow.com/questions/50583705/late-binding-vs-early-binding-in-vba-createobject-vs-new – K.Dᴀᴠɪs Nov 28 '18 at 17:29
  • 1
    Found the answer here! – Dani Nov 28 '18 at 17:30

1 Answers1

2

FileSystemObject requires a reference to the Microsoft Scripting Runtime library.

Look at the references in your previous workbook.

Tim Williams
  • 154,628
  • 8
  • 97
  • 125