0

我一直在尝试编写一个 VBA 脚本,它可以解析出其他文件中的特定数据,然后我可以将这些文件输入到当前打开的 Excel 工作簿中。这些文件都是制表符分隔的,并且位于与当前打开的工作簿不同的目录中。下面的代码一直有效,直到我重新启动计算机,现在我总是收到“1004 运行时错误”。当代码命中 DeliminateCSV 子例程的“select.TextToColumns”行时,总是会发生此错误。发生错误是因为程序正在选择空单元格。我相信该程序正在选择当前打开的当前为空的工作簿。我相信 ParseSummaryReport 中的 CSV 变量可能打开了错误的工作簿,但我在调试模式下观看了它,它似乎有正确的文件路径可以打开。有什么想法吗?

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
4

1 回答 1

3

当你使用

With wrkBook
...
End with

那么你应该添加一个“。” 在属于该工作簿的任何属性或方法之前

With wrkBook
    Columns("A:A").Select             
    Selection.TextToColumns Destination:=Range("A1")...
End With

应该

With wrkBook
    .Columns("A:A").Select             
    Selection.TextToColumns Destination:=.Range("A1")...
End With

您不需要选择:

.Columns("A:A").TextToColumns Destination:=.Range("A1")...

也将工作

于 2012-06-22T22:57:47.067 回答