*** 作设备:戴尔电脑
*** 作系统:win10
*** 作软件:Excel2016
1、将需合并的Excel文件放在同一个文件夹中。
2、打开新建的Excel文件,按 Alt + F11 键,打开宏,选择视图→代码窗口。
3、将下面的代码拷贝粘贴到代码窗口中:
Sub sheets2one()
'定义对话框变量
Dim cc As FileDialog
Set cc = ApplicationFileDialog(msoFileDialogFilePicker)
Dim newwork As Workbook
Set newwork = WorkbooksAdd
With cc
If Show = -1 Then
Dim vrtSelectedItem As Variant
Dim i As Integer
i = 1
For Each vrtSelectedItem In SelectedItems
Dim tempwb As Workbook
Set tempwb = WorkbooksOpen(vrtSelectedItem)
tempwbWorksheets(1)Copy Before:=newworkWorksheets(i)
newworkWorksheets(i)Name = VBAReplace(tempwbName, "xls", "")
tempwbClose SaveChanges:=False
i = i + 1
Next vrtSelectedItem
End If
End With
Set cc = Nothing
End Sub
4、点击菜单“运行”—“运行子过程/用户窗体”。
5、在d出选择对话框中,选择要批量合并的Excel文件,点击“确定”即可合并为一个Excel工作簿。
6、点击“确定”后,运行合并即可。
Sub 汇集数据()Dim s As Worksheet
c=1
For Each s In ThisWorkbookWorksheets
With s
If Name <> "汇总表" Then '假设存放结果的工作表叫“汇总表”,如果不是请做相应修改
c = c + 1
For i=1 to 10 '假设你有10列数据要拷到汇总表
Sheets("汇总表")Cells(c, i) =sCells(37, i)
Next i
End If
End With
Next
End Sub
代码如下:
Option ExplicitSub 把所有工作表内容合并到最后一个工作表中()
Dim i, n, st As Worksheet
For i = SheetsCount to 1 Step -1
If i = SheetsCount Then
Set st = Sheets(i)
If stUsedRangeCellsCount = 1 And stCells(1, 1) = "" Then
n = 1 '下一次添加内容的行
Else
n = stUsedRangeRowsCount + 1
End If
Else
Sheets(i)UsedRangeCopy stCells(n, 2)
stCells(n, 1)resize(Sheets(i)UsedRangeRowsCount,1)=sheets(i)name
n = stUsedRangeRowsCount + 1
End If
Next i
End Sub
*** 作中遇到问题,请百度云联系。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)