1

我有 3 个 csv 文件,我必须合并,但在此之前我必须准备具有相同的列顺序。一切正常,除了我无法弄清楚输出文件中列的更改顺序。我可以选择我想要的列和其他跳过很好但如果我想交换一些怎么办?我认为在 fieldinfo 数组中放置正确的顺序可以解决问题,但没有。我想用 6 交换 8。

    Application.ScreenUpdating = False
    Workbooks.OpenText Filename:=Filenamenew, Origin:=xlWindows, StartRow _
    :=2, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
    ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=True, _
    Space:=False, Other:=False, FieldInfo:=Array(Array(1, 3), Array(2, 9), Array(3, 1), Array(4, 9), Array(5, 9), Array(8, 1), Array(7, 9), Array(6, 1))

    Set Wb = ActiveWorkbook
    Application.DisplayAlerts = False
    Wb.SaveAs Filename:=LBname, FileFormat:=FileFormatNum, _
    ReadOnlyRecommended:=False, _
    CreateBackup:=False
    Application.DisplayAlerts = True

    Wb.Close savechanges:=False

谢谢

4

1 回答 1

2

最好的方法是更改​​创建需要更改的 CSV 文件的任何内容的输出。我假设您出于某种原因无权访问它,因此这将完成工作:

Sub swapColumns(first As Integer, second As Integer)
    Dim wb As Workbook
    Dim ws As Worksheet

    Set wb = Workbooks.Open("C:\root\test.csv")
    Set ws = wb.Sheets(1)
    If first > second Then
        Dim i As Integer
        i = first
        first = second
        second = i
    ElseIf first = second Then
        Exit Sub
    End If
    ws.Columns(second).Cut
    ws.Columns(first).Insert Shift:=xlToRight
    ws.Columns(first + 1).Cut
    ws.Columns(second + 1).Insert Shift:=xlToRight
End Sub

您可以使用现有代码调用此 SubswapColumns 6, 8

于 2012-12-03T00:14:46.737 回答