在Excel中导入和排序文本文件

问题描述 投票:0回答:1

我目前有一个宏设置,可让我从Textpad将数据粘贴到A列中,然后对数据进行排序,四舍五入,移动并将其保存为txt文件。

无论如何,我是否可以修改宏以允许将txt文件直接导入Excel,而不必复制和粘贴?文件名每次都会更改,但是文件目录将保持不变。

这是我手动复制并将数据粘贴到excel后运行的当前宏:

    'Sub SortRoundandSave()
    '
    ' SortTruncateandSave Macro
    ' This macro will sort, round and save your data
    '

    '
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
    :=Array(Array(1, 1), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True
Range("D1").Select
Selection.AutoFill Destination:=Range("D1:D" & Range("B" & Rows.Count).End(xlUp).Row)
Range("A:A,B:B,D:D").Select
Range("D1").Activate
Selection.Copy
Sheets("Truncated Data").Select
ActiveSheet.Paste
Sheets("Truncated Data").Select
Application.CutCopyMode = False
Sheets("Truncated Data").Move
ChDir "G:\XXXX\Folder\Name\ZZZZ\Sort"
ActiveWorkbook.SaveAs Filename:= _
    "G:\XXXX\Folder\Name\ZZZZ\Sort\Sorted Data.txt", _
    FileFormat:=xlText, CreateBackup:=False
Windows("Excel Truncator.xlsm").Activate
    End Sub

我认为在文本到列的部分之前需要几行,但是我似乎无法工作?

任何帮助将不胜感激。

谢谢

excel vba sorting import
1个回答
0
投票

尝试一下:

Sub SortRoundandSave()
    '
    ' SortTruncateandSave Macro
    ' This macro will sort, round and save your data
    '
    Dim workSht As Worksheet: Set workSht = ActiveSheet 'ThisWorkbook.Sheets("") ' Enter the name of sheet

    Dim FilePath As String
    Dim strLine As String
    Dim rowCnt As Long

    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Select the file"
        .Filters.Clear
        .Filters.Add Description:="Text Files", Extensions:="*.txt"
        '.InitialFileName = "G:\XXXX\Folder\Name" ' Optional: this is a startup directory, place the correct one and uncomment line
        If .Show = 0 Then Exit Sub
        FilePath = .SelectedItems(1)
    End With

    rowCnt = 1

    Open FilePath For Input As #1
        Do While Not EOF(1)
            Line Input #1, strLine
            workSht.Cells(rowCnt, 1) = strLine
            rowCnt = rowCnt + 1
        Loop
    Close #1


    With workSht
        Range(.Cells(1, 1), .Cells(rowCnt - 1, 1)).TextToColumns Destination:=.Cells(1, 1), DataType:=xlDelimited, _
                                TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
                                Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
                                :=Array(Array(1, 1), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True

        .Cells(1, 4).AutoFill Destination:=Range(.Cells(1, 4), .Cells(workSht.Cells(Rows.Count, 2).End(xlUp).Row, 4)) ' "D1:D" & Range("B" & Rows.Count).End(xlUp).Row)

        Range(.Cells(1, 1), .Cells(1, 4)).EntireColumn.Copy Sheets("Truncated Data").Cells(1, 1)
    End With

    Sheets("Truncated Data").Move

    ActiveWorkbook.SaveAs Filename:= _
    "G:\XXXX\Folder\Name\ZZZZ\Sort\Sorted Data.txt", _
    FileFormat:=xlText, CreateBackup:=False

    workSht.Parent.Activate

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.