|
- Sub on1_Click()
- Dim MyPath As String, FilesInPath As String
- Dim MyFiles() As String
- Dim SourceR As String
- Dim wb As Workbook
- Dim DestR As Range
- Dim i As Integer, j As Integer
-
- '设置源数据范围
- SourceR = Range("A1:B60").Address
-
- '设置目标数据范围
- Set DestR = ActiveSheet.Range("A1")
-
- '获取当前文件夹路径
- MyPath = ActiveWorkbook.Path & ""
-
- '获取当前文件夹下的所有 Excel 文件
- FilesInPath = Dir(MyPath & "*.xls*")
- i = 0
-
- '循环处理每个 Excel 文件
- Do While FilesInPath <> "" And FilesInPath <> ThisWorkbook.Name
- If FilesInPath <> ThisWorkbook.Name Then
- i = i + 1
- ReDim Preserve MyFiles(1 To i)
- MyFiles(i) = FilesInPath
- FilesInPath = Dir()
- End If
- Loop
-
- '循环打开每个 Excel 文件,复制数据到目标工作薄
- For j = 1 To UBound(MyFiles)
- Application.ScreenUpdating = False
- Set wb = Workbooks.Open(MyPath & MyFiles(j), 0)
- wb.Sheets(1).Range(SourceR).Copy
- DestR.Offset((j - 1) * 60, 0).PasteSpecial xlPasteValues
- Application.CutCopyMode = False
- wb.Close , False
- Next j
- Application.ScreenUpdating = True
- MsgBox "数据已复制完成。"
- End Sub
复制代码 |
|