【发布时间】:2015-09-18 19:27:24
【问题描述】:
更新:
以下是我在joinedupdata.com 上找到的示例VBA 代码。我需要帮助进行两项修改:(1)删除删除重复标题行的条件,(2)查看是否有办法将每个 Excel 文件中的连接数据通过文件名的组合工作表中的空白行分开下表在最左边的单元格中。
Dim firstRowHeaders As Boolean
Dim fso As Object
Dim dir As Object
Dim filename As Variant
Dim wb As Workbook
Dim s As Sheet1
Dim thisSheet As Sheet1
Dim lastUsedRow As Range
Dim file As String
On Error GoTo ErrMsg
Application.ScreenUpdating = False
firstRowHeaders = True 'Change from True to False if there are no headers in the first row
Set fso = CreateObject("Scripting.FileSystemObject")
'PLEASE NOTE: Change <<Full path to your Excel files folder>> to the path to the folder containing your Excel files to merge
Set dir = fso.Getfolder("<<Full path to your Excel files folder>>")
Set thisSheet = ThisWorkbook.ActiveSheet
For Each filename In dir.Files
'Open the spreadsheet in ReadOnly mode
Set wb = Application.Workbooks.Open(filename, ReadOnly:=True)
'Copy the used range (i.e. cells with data) from the opened spreadsheet
If firstRowHeaders And i > 0 Then 'Only include headers from the first spreadsheet
Dim mr As Integer
mr = wb.ActiveSheet.UsedRange.Rows.Count
wb.ActiveSheet.UsedRange.Offset(1, 0).Resize(mr - 1).Copy
Else
wb.ActiveSheet.UsedRange.Copy
End If
'Paste after the last used cell in the master spreadsheet
If Application.Version < "12.0" Then 'Excel 2007 introduced more rows
Set lastUsedRow = thisSheet.Range("A65536").End(xlUp)
Else
Set lastUsedRow = thisSheet.Range("A1048576").End(xlUp)
End If
'Only offset by 1 if there are current rows with data in them
If thisSheet.UsedRange.Rows.Count > 1 Or Application.CountA(thisSheet.Rows(1)) Then
Set lastUsedRow = lastUsedRow.Offset(1, 0)
End If
lastUsedRow.PasteSpecial
Application.CutCopyMode = False
Next filename
ThisWorkbook.Save
Set wb = Nothing
#If Mac Then
'Do nothing. Closing workbooks fails on Mac for some reason
#Else
'Close the workbooks except this one
For Each filename In dir.Files
file = Right(filename, Len(filename) - InStrRev(filename, Application.PathSeparator, , 1))
Workbooks(file).Close SaveChanges:=False
Next filename
#End If
Application.ScreenUpdating = True
ErrMsg:
If Err.Number <> 0 Then
MsgBox "There was an error. Please try again. [" & Err.Description & "]"
End If
我一直在尝试(但没有取得多大成功)找到一种将多个 Excel 电子表格合并为一个的方法。我正在使用 MATLAB 分析实验数据。输入一打 Excel 电子表格,输出等量。
电子表格结构:
每个 Excel 文件中的数据仅在第一张表(Sheet 1)上。
每张工作表有四列数据(带有标题)和下面可变数量的数据行。
每个 Excel 文件都有一个唯一的文件名。
示例:
Header 1 | Header 2 | Header 3 | Header 4
1111 22222 3333 4444
11122 11223 33344 33444
etc etc etc etc
首选合并行为:
1) 多个 Excel 文件合并到一个新电子表格上的一个工作表中。
2) 在合并期间维护列标题。
3) 与其将每个连续数据集添加到前一个数据集的底部(“垂直”添加),不如将列与一个并排放置(“水平”添加) - 中间的分栏。
4) 每个原始文件的文件名放在第一列标题上方的一行中。
5) 最好是跨平台的(Windows/Mac OS X)。但是,如果 VBA 和 ActiveX 是唯一的方法,那也没关系。
样本输出:
Filename1 Filename2
Header 1 | Header 2 | Header 3 | Header 4 Header 1 | Header 2 | Header 3 | ...
111 22222 33333 4444 1111 222222 44444
Data... Data... Data... Data... Data... Data... Data...
【问题讨论】:
-
你可以用 VBA 做到这一点,有没有你遇到问题的特定部分?
-
您使用的是什么版本的 Excel?另外,您想将多少个 Excel 文件连接在一起的上限是多少?请记住,根据您保存的版本和扩展名,您可以拥有的列数会受到限制。但只要你不做超过 3000 个文件,你应该没问题。请包括您迄今为止所做的任何工作。一个好的起点是使用宏记录器,然后进入编辑器并开始适应您的需求。
-
我没有使用 VBA 编码的经验。因此,如果您能指出我可以调整的工作代码,那就太好了。另外,我在 Mac 上:这使得运行包含 ActiveX 的宏变得困难。
-
@nbayly:我使用的是 MS Office for Mac 2011。一次最多需要加入 15 个文件。我目前没有工作代码,因为我没有 VBA 经验。
-
您将如何提供有关要加入哪些文件(文件名、目录等)的脚本信息?
标签: matlab excel concatenation vba