Excel精英培训网

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

[已解决]VBA实现多工作簿数据整理到一个工作簿内

[复制链接]
发表于 2016-11-23 12:14 | 显示全部楼层 |阅读模式
本帖最后由 摩米 于 2016-11-24 13:14 编辑

详情在附件内,VBA小白 请求老师帮忙解决!
"原始数据"文件里面的工作簿都是下载后没有动过的,点开都是需要启用编辑
批量导入"原始数据"文件夹中 所有工作簿数据到"模板"工作簿,并且对应日期
还需要把文本转换成数值

因为每天都需要大量时间来把文本转换为数值,然后复制粘贴,还请老师费心编写一下,万分感谢!


最佳答案
2016-11-23 14:07
  1. Sub tt()
  2. Dim filePath As String, shName As String
  3. Dim wbName As String, wb As Workbook, sh As Worksheet
  4. Dim lstRow%, staRow%, mbRow&, i%, n%
  5. Application.ScreenUpdating = False
  6. Sheet1.Range("a2:m65536").ClearContents
  7. filePath = ThisWorkbook.Path & "\原始数据"
  8. wbName = Dir(filePath & "*.xls*")
  9. Do While wbName <> ""
  10.     Set wb = Workbooks.Open(filePath & wbName, True, True)
  11.     For Each sh In wb.Worksheets
  12.         staRow = sh.Range("A:A").Find("关键词").Row + 2
  13.         lstRow = sh.Range("a65536").End(3).Row
  14.         mbRow = Sheet1.Range("a65536").End(3).Row
  15.         arr = sh.Range(Cells(staRow, 1), Cells(lstRow, 13))
  16.         n = 0
  17.             For i = 1 To UBound(arr)
  18.             If arr(i, 1) <> "" Then
  19.                 n = n + 1
  20.                 Sheet1.Cells(mbRow + n, 1) = Right(Split(wbName, ".")(0), 10)
  21.                 Sheet1.Cells(mbRow + n, 2).Resize(, 13) = Application.Index(arr, i)
  22.             End If
  23.             Next
  24.         wb.Close: Set wb = Nothing: Erase arr
  25.     Next
  26.     wbName = Dir
  27. Loop
  28. Application.ScreenUpdating = True
  29. End Sub
复制代码

试做了下,单元格格式没有设置
发表于 2016-11-23 13:31 | 显示全部楼层
模板里日期有何用?前三列插入一些文件名及表名信息。希望能对你有帮助。

模板.zip

116.37 KB, 下载次数: 13

回复

使用道具 举报

发表于 2016-11-23 14:07 | 显示全部楼层    本楼为最佳答案   
  1. Sub tt()
  2. Dim filePath As String, shName As String
  3. Dim wbName As String, wb As Workbook, sh As Worksheet
  4. Dim lstRow%, staRow%, mbRow&, i%, n%
  5. Application.ScreenUpdating = False
  6. Sheet1.Range("a2:m65536").ClearContents
  7. filePath = ThisWorkbook.Path & "\原始数据"
  8. wbName = Dir(filePath & "*.xls*")
  9. Do While wbName <> ""
  10.     Set wb = Workbooks.Open(filePath & wbName, True, True)
  11.     For Each sh In wb.Worksheets
  12.         staRow = sh.Range("A:A").Find("关键词").Row + 2
  13.         lstRow = sh.Range("a65536").End(3).Row
  14.         mbRow = Sheet1.Range("a65536").End(3).Row
  15.         arr = sh.Range(Cells(staRow, 1), Cells(lstRow, 13))
  16.         n = 0
  17.             For i = 1 To UBound(arr)
  18.             If arr(i, 1) <> "" Then
  19.                 n = n + 1
  20.                 Sheet1.Cells(mbRow + n, 1) = Right(Split(wbName, ".")(0), 10)
  21.                 Sheet1.Cells(mbRow + n, 2).Resize(, 13) = Application.Index(arr, i)
  22.             End If
  23.             Next
  24.         wb.Close: Set wb = Nothing: Erase arr
  25.     Next
  26.     wbName = Dir
  27. Loop
  28. Application.ScreenUpdating = True
  29. End Sub
复制代码

试做了下,单元格格式没有设置
回复

使用道具 举报

 楼主| 发表于 2016-11-23 14:08 | 显示全部楼层
yanhj 发表于 2016-11-23 13:31
模板里日期有何用?前三列插入一些文件名及表名信息。希望能对你有帮助。

日期是我后面还需要对数据进行处理的,是一定需要的。您的代码还没有解决我的问题,不过还是谢谢您
回复

使用道具 举报

 楼主| 发表于 2016-11-23 14:30 | 显示全部楼层
苏子龙 发表于 2016-11-23 14:07
试做了下,单元格格式没有设置

非常的好,正是我想要的结果 好想快点学会,万分感谢 谢谢  谢谢!!{:021:}
回复

使用道具 举报

发表于 2016-11-23 14:42 | 显示全部楼层
x = [D65536].End(3).Row  改  x = [E65536].End(3).Row

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-24 04:26 , Processed in 0.338115 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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