假設在每一個文件你的工作表(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
然後輸入給定的代碼到這個文件,當輸出文件是活動工作表時運行它。
,謝謝。我試過它沒有工作。在我所有的文件數據都在sheet1中。 – davidb
你是否在'mDirs =「c:\」'你的路徑在這裏用\在最後'輸入了你的xlsx文件的文件夾名? 例如:「c:\ data \ xlsFiles \」 – tretom
是我按照您的示例輸入了路徑「c:\ data \ xlsFiles \」 – davidb