I have been attempting to write a VBA Script that can parse out other files for specific data that I can then input into the currently opened Excel Workbook. The files are all Tab Deliminated and are found in a different directory than the currently opened workbook. The code below worked until I restarted my computer, and now I always get a “1004 Runtime Error.” This error always occurs when the code hits the “select.TextToColumns” line of the DeliminateCSV subroutine. The error occurs because the program is selecting empty cells. I believe the program is selecting the currently open workbook which is currently empty. I believed maybe my CSV variable in ParseSummaryReport was opening the wrong workbook, but I watched it in Debug mode and it seemed to have the correct filepath to open. Any ideas whats going on?
Option Explicit
Sub PopulateSpreadSheet()
Dim fso As Object
Dim fPath As String
Dim fsoFolder As Scripting.folder
Dim startingFolder As Scripting.folder
Dim iNumFiles As Integer
Set fso = CreateObject("Scripting.FileSystemObject")
'Get path of current workbook
fPath = ActiveWorkbook.Path
Set fsoFolder = fso.GetFolder(fPath)
'Move up one directory
Set startingFolder = fsoFolder.ParentFolder
iNumFiles = 0
Call RecursiveFileCheck(startingFolder, iNumFiles)
End Sub
Sub ParseSummaryReport(ByRef i As Integer, fileName As String)
Dim CSV As Workbook
Dim Excel As Excel.Application
'Data From CSV To Put In Tracking
Dim ES_Number As String
Dim custodian As String
Dim EDoc_Size As Double
Dim Email_Size As Double
Set Excel = New Excel.Application
'Set CSV = Excel.Workbooks.Open(fileName)
Set CSV = Excel.Workbooks.Open(fileName, , , , , , , , " ")
'Deliminate the CSV File
'Call DeliminateCSV(CSV)
Call CSV.Close(False)
End Sub
Sub RecursiveFileCheck(ByRef folder As Scripting.folder, ByRef iNumFiles As Integer)
Dim nextFolder As Scripting.folder
Dim fileName As String
Dim nextFile, files, subFolders
Set files = folder.files
Set subFolders = folder.subFolders
'Search through all the files in this folder
For Each nextFile In files
'Check if this is one of the files we want
If nextFile Like "*_SummaryReport.csv" Then
'Summary Report Found, Parse It
fileName = nextFile
Call ParseSummaryReport(iNumFiles, fileName)
End If
Next nextFile
'Search through all the subfolders recursively
For Each nextFolder In subFolders
Call RecursiveFileCheck(nextFolder, iNumFiles)
Next nextFolder
End Sub
Sub DeliminateCSV(ByRef wrkBook As Workbook)
With wrkBook
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
End With
End Sub
When you use
then you should add a “.” in front of any properties or methods belonging to that workbook
should be
You don’t need to select though:
will also work