Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2224|回复: 6

[已解决]如何能将若干excel文件按照模板要就汇成一个新表

[复制链接]
发表于 2013-10-16 11:06 | 显示全部楼层 |阅读模式
本帖最后由 laye 于 2013-10-16 11:39 编辑

如何能将若干excel文件按照模板要就汇成一个新表,已有的excel文件格式是相同的,内容不同且文件个数不定,新生成的excel文件格式固定,无需打印。上传两个附件,求帮助……

最佳答案
2013-10-16 12:40
新文件模板.rar (12.84 KB, 下载次数: 13)

新文件模板.zip

3.79 KB, 下载次数: 18

原文件.zip

35.5 KB, 下载次数: 16

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-10-16 11:16 | 显示全部楼层
第一个关键点是文件遍历的问题。
剩下的就很容易了,提取数据。

关于遍历可查下论坛或F1 DIR。

回复

使用道具 举报

 楼主| 发表于 2013-10-16 11:40 | 显示全部楼层
原有文件存在格式兼容问题可能会打不开,现重新上传了“原有文件”附件。
回复

使用道具 举报

发表于 2013-10-16 12:26 | 显示全部楼层
  1. Sub 数据整理()
  2.     Dim strPath As String, strFile As String
  3.     Dim arr(1 To 10000, 1 To 6), i As Long
  4.     strPath = ThisWorkbook.Path & Application.PathSeparator & Application.PathSeparator
  5.     strFile = Dir(strPath & "*.xls")
  6.     With Application
  7.         .ScreenUpdating = False
  8.         .DisplayAlerts = False
  9.         .Calculation = xlCalculationManual
  10.     End With
  11.     Do While Len(strFile)
  12.         If strFile <> ThisWorkbook.Name Then
  13.             '找到文件后执行的操作
  14.             Debug.Print strFile
  15.             Call getDate(strPath & strFile, arr, i)
  16.         End If
  17.         strFile = Dir
  18.     Loop
  19.     Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(i, UBound(arr, 2)).Value = arr
  20.     With Application
  21.         .ScreenUpdating = True
  22.         .DisplayAlerts = True
  23.         .Calculation = xlCalculationAutomatic
  24.     End With
  25.     MsgBox "数据整理完成", vbInformation
  26. End Sub

  27. Sub getDate(ByRef strFullName As String, ByRef arr As Variant, ByRef arrSize As Long)
  28.     Dim objWbk As Workbook
  29.     Dim arrTemp
  30.     Dim strCode$, strCompany$
  31.     Dim lLastrow As Long
  32.     Dim i As Long
  33.     Set objWbk = GetObject(strFullName)
  34.     With objWbk
  35.         With .Worksheets("sheet1")
  36.             lLastrow = .Cells(Rows.Count, "b").End(xlUp).Row
  37.             If lLastrow > 4 Then
  38.                 arrTemp = .Range("a5:e" & lLastrow).Value
  39.                 strCode = Split(.Range("a2").Value, ":")(1)
  40.                 strCompany = Split(.Range("a3").Value, ":")(1)
  41.                 For i = LBound(arrTemp) To UBound(arrTemp)
  42.                     arrSize = arrSize + 1
  43.                     arr(arrSize, 1) = arrTemp(i, 1)
  44.                     arr(arrSize, 2) = "'" & strCode
  45.                     arr(arrSize, 3) = "'" & strCompany
  46.                     arr(arrSize, 4) = arrTemp(i, 3)
  47.                     arr(arrSize, 5) = "'" & arrTemp(i, 4)
  48.                     arr(arrSize, 6) = arrTemp(i, 5)
  49.                 Next
  50.             End If
  51.         End With
  52.         .Close False
  53.     End With
  54. End Sub
复制代码
回复

使用道具 举报

发表于 2013-10-16 12:38 | 显示全部楼层
序列号修正了下,原来引用的分表里的。
  1. Sub 数据整理()
  2.     Dim strPath As String, strFile As String
  3.     Dim arr(1 To 10000, 1 To 5), i As Long, j As Long
  4.     strPath = ThisWorkbook.Path & Application.PathSeparator & Application.PathSeparator
  5.     strFile = Dir(strPath & "*.xls")
  6.     With Application
  7.         .ScreenUpdating = False
  8.         .DisplayAlerts = False
  9.         .Calculation = xlCalculationManual
  10.     End With
  11.     Do While Len(strFile)
  12.         If strFile <> ThisWorkbook.Name Then
  13.             Call getDate(strPath & strFile, arr, i)
  14.         End If
  15.         strFile = Dir
  16.     Loop
  17.    
  18.     Cells(Rows.Count, "b").End(xlUp).Offset(1).Resize(i, UBound(arr, 2)).Value = arr
  19.     With Range("a2")
  20.         .Value = 1
  21.         .AutoFill .Resize(Cells(Rows.Count, "b").End(xlUp).Row - 1), xlFillSeries
  22.     End With
  23.    
  24.     With Application
  25.         .ScreenUpdating = True
  26.         .DisplayAlerts = True
  27.         .Calculation = xlCalculationAutomatic
  28.     End With
  29.     MsgBox "数据整理完成", vbInformation
  30. End Sub

  31. Sub getDate(ByRef strFullName As String, ByRef arr As Variant, ByRef arrSize As Long)
  32.     Dim objWbk As Workbook
  33.     Dim arrTemp
  34.     Dim strCode$, strCompany$
  35.     Dim lLastrow As Long
  36.     Dim i As Long
  37.     Set objWbk = GetObject(strFullName)
  38.     With objWbk
  39.         With .Worksheets("sheet1")
  40.             lLastrow = .Cells(Rows.Count, "b").End(xlUp).Row
  41.             If lLastrow > 4 Then
  42.                 arrTemp = .Range("a5:e" & lLastrow).Value
  43.                 strCode = Split(.Range("a2").Value, ":")(1)
  44.                 strCompany = Split(.Range("a3").Value, ":")(1)
  45.                 For i = LBound(arrTemp) To UBound(arrTemp)
  46.                     arrSize = arrSize + 1
  47.                     arr(arrSize, 1) = "'" & strCode
  48.                     arr(arrSize, 2) = "'" & strCompany
  49.                     arr(arrSize, 3) = arrTemp(i, 3)
  50.                     arr(arrSize, 4) = "'" & arrTemp(i, 4)
  51.                     arr(arrSize, 5) = arrTemp(i, 5)
  52.                 Next
  53.             End If
  54.         End With
  55.         .Close False
  56.     End With
  57. End Sub
复制代码
回复

使用道具 举报

发表于 2013-10-16 12:40 | 显示全部楼层    本楼为最佳答案   
新文件模板.rar (12.84 KB, 下载次数: 13)
回复

使用道具 举报

 楼主| 发表于 2013-10-16 13:09 | 显示全部楼层
测试OK,完全符合需要,谢谢。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-20 12:57 , Processed in 0.302886 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表