I just wrote my first VBA script for Excel because I have to write many "*.txt" files from a folder into an excel spreadsheet. But when I run this script, I get the error '5018'. It is invoked by the line
If reg.Test(file.Name) Then
Any idea what I am doing wrong? Here is the complete script:
Sub get_filenames()
Set fso = CreateObject("Scripting.FileSystemObject")
Set Files = fso.GetFolder("C:\myfolder").Files
Set reg = CreateObject("vbscript.regexp")
reg.IgnoreCase = True
reg.MultiLine = False
reg.Pattern = "*.txt"
For Each file In Files
If reg.Test(file.Name) Then
i = i + 1
Cells(i, 1) = file.Name
End If
Next
End Sub