重新排列多个Excel文件中的列

时间:2017-02-20 08:55:10

标签: excel vba excel-vba

我在.xlsx扩展名中有100多个excel文件,所有文件中的列都没有按顺序排列,我想按照我的模板重新排列列顺序,我想追加所有文件中的数据到一个输出文件。

我已尝试过此链接中的解决方案Rearranging Columns in Multiple Excel Files using VBA但它无效。

以下是样本文件标题供参考。

File1中

标题1,Heading2,Heading3

文件2

Heading2,标题1,Heading5,Heading7

模板文件

标题1,Heading2,Heading3,Heading4,Heading5,Heading6,Heading7

预期输出文件

的文件名,标题1,Heading2,Heading3,Heading4,Heading5,Heading6,Heading7

2 个答案:

答案 0 :(得分:2)

尝试以下方法。

Sub Order_Columns()
    Dim template_headers As Variant, header As Variant, current_header As Variant, cl As Range, col As Integer

    template_headers = Array("Heading1", "Heading2", "Heading3", "Heading4", "Heading5")

    For header = LBound(template_headers) To UBound(template_headers)
        current_header = template_headers(header)

        col = col + 1
        Set cl = ActiveSheet.Rows(1).Find(What:=current_header, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

        If Not cl Is Nothing Then
            If Not cl.Column = col Then
                Columns(cl.Column).Cut
                Columns(col).Insert Shift:=xlToRight
            End If
        End If
    Next header
End Sub
  • 在数组中指定所需的标题顺序
  • 请注意,标题区分大小写,因此可以使用LCase()

我将与您一起添加代码以循环遍历您的100多个文件夹,然后将这些数据放入主表格中!

答案 1 :(得分:1)

假设您在每个文件中处理工作表(1) 这样做可以做到:

Option Explicit
Sub ColumnMover()

    Dim i As Integer, j As Integer, k As Integer, m As Integer, n As Integer
    Dim mDirs As String
    Dim path As String
    Dim OutFile As Variant, SrcFile As Variant
    Dim MyObj As Object, MySource As Object, file As Variant

    OutFile = ActiveWorkbook.Name
        mDirs = "c:\" 'your path here with \ in the end
        file = Dir(mDirs)
        While (file <> "")
            path = mDirs + file
            Workbooks.Open (path)
            SrcFile = ActiveWorkbook.Name

            n = 2
            While Workbooks(OutFile).Sheets(1).Cells(n, 1).Value <> ""
                n = n + 1
            Wend

            i = 2
            While (Workbooks(OutFile).Sheets(1).Cells(1, i).Value <> "")
                k = n
                j = 1
                While Workbooks(SrcFile).Sheets(1).Cells(1, j).Value <> Workbooks(OutFile).Sheets(1).Cells(1, i).Value And _
                      Workbooks(SrcFile).Sheets(1).Cells(1, j).Value <> ""

                    j = j + 1
                Wend

                If Workbooks(SrcFile).Sheets(1).Cells(1, j).Value = Workbooks(OutFile).Sheets(1).Cells(1, i).Value Then

                    m = 2
                    While Workbooks(SrcFile).Sheets(1).Cells(m, j).Value <> ""

                        Workbooks(OutFile).Sheets(1).Cells(k, 1).Value = path
                        Workbooks(OutFile).Sheets(1).Cells(k, i).Value = Workbooks(SrcFile).Sheets(1).Cells(m, j).Value

                        k = k + 1
                        m = m + 1
                    Wend
                End If

                i = i + 1
            Wend

            Workbooks(file).Close (False)
            file = Dir
        Wend
End Sub

编辑:

一些解释:

这里的模板文件和输出文件是相同的。首先,你必须在表(1)上有一个xlsm结构:

的文件名,标题1,Heading2,Heading3,Heading4,Heading5,Heading6,Heading7

然后将给定代码输入此文件,并在输出文件为活动工作表时运行它。