VBA导入和分割分隔的文本文件

时间:2019-03-13 21:00:11

标签: excel vba

我有一些制表符分隔的文件,需要导入,然后仅提取某些信息并将其粘贴到我的工作簿中。我写了以下Sub,它可以工作,但是速度很慢。我必须想象这是因为for循环,但是我对此很陌生,这是我能想到的最好的方法。我看到使用了EOF,但拆分后可以使用吗?

 var url = new Uri("http://tfsserver:8080/tfs");
 TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(url);
 BuildHttpClient client = ttpc.GetClient<BuildHttpClient>();
 List<Build> builds = client.GetBuildsAsync().Result;

2 个答案:

答案 0 :(得分:1)

通过将所有数据收集到一个数组中,然后一次性将所有数据写入工作表中,可以达到最佳速度。

您的代码中还有许多其他问题,请参见下面标有<~~~的嵌入说明

Sub FindResults()
    'Selecting file for import
    Dim FileSelect As FileDialog '<~~~ use explicit type
    Dim PlateMapFolder As String

    '<~~~ declare all Variables
    Dim SelectedFile As String
    Dim strFileContent As String
    Dim strResults() As String
    Dim arrResultsLine() As String
    Dim arrResultsTab() As String
    Dim i As Long

    PlateMapFolder = "C:\"
    Set FileSelect = Application.FileDialog(msoFileDialogFilePicker)
    With FileSelect
        .InitialFileName = PlateMapFolder
        .AllowMultiSelect = False
        .Title = "Please select associated run"
        .Show

        If .SelectedItems.Count = 0 Then
            Exit Sub
        End If

        'SelectedFile = Dir(.SelectedItems(1)) '<~~~ No need for Dir here
        SelectedFile = .SelectedItems(1)
    End With

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    'Splitting SelectedFile
    Const strSearch = "[Results]"
    Dim intFileNumber As Integer

    intFileNumber = FreeFile

    Open SelectedFile For Input As intFileNumber
    strFileContent = Input(LOF(intFileNumber), intFileNumber)
    Close intFileNumber '<~~~ close file after use

    'Split result file at [Results]
    strResults = Split(strFileContent, strSearch)

    'Split line breaks
    arrResultsLine = Split(strResults(1), vbLf)

    'Split each line by tab

    ' <~~~ declare and size array to hold results
    Dim Res As Variant
    ReDim Res(1 To UBound(arrResultsLine) - 2, 1 To 5)
    'intRow = 1 <~~~ not needed

    '<~~~ this will skip first and last line after [Results].
    '     Is this what you want?
    '     If not, also adjust Redim size
    For i = 2 To UBound(arrResultsLine) - 1
        arrResultsTab = Split(arrResultsLine(i), vbTab)
        '<~~~ collect data into array
        Res(i - 1, 1) = arrResultsTab(0)
        Res(i - 1, 2) = arrResultsTab(1)
        Res(i - 1, 3) = arrResultsTab(2)
        Res(i - 1, 4) = arrResultsTab(3)
        Res(i - 1, 5) = arrResultsTab(4)
    Next i
    '<~~~ write to sheet in one go
    Sheets("RawData").Range("A1").Resize(UBound(Res, 1), UBound(Res, 2)).Value = Res

    '<~~~ turn these back on!
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

答案 1 :(得分:0)

逐个单元地写入非常慢,因此会更快。

For i = 2 To UBound(arrResultsLine) - 1

     arr = Split(arrResultsLine(i), vbTab)

     'write the data as an array
     Sheets("RawData").Cells(intRow, "A").Resize(1, 5).Value = _
           Array(arr(0), arr(1), arr(2), arr(3), arr(4))

     intRow = intRow + 1

Next i

如果您需要更高的速度,请创建一个包含所有数据的二维数组,然后通过一次操作将其直接写入工作表。