Office中国论坛/Access中国论坛
标题:
急救!!! VBA 批量导入和批量导出标准表格
[打印本页]
作者:
鱼儿5610
时间:
2016-9-4 19:21
标题:
急救!!! VBA 批量导入和批量导出标准表格
各位高手:
最近手上有几千行数据(见索赔明细),每一行代表对一个供应商的索赔信息(包括供应商名称、原始金额、供应商名称、索赔单编号、索赔产品、数量、发生月份等),需要将索赔明细中每一行的信息对应复制黏贴到标准表格对应的阴影区域,由于数据量太大。而且是重复的动作,不知道可以用VBA实现吗?急用,万分感谢
其中标准表格中阴影区域的数据来源如下:
GX2016090476 来源于 索赔明细中 索赔单编号
2016-9-2 来源于 索赔明细中 制单日期
黎明液压有限公司 来源于 索赔明细中 供应商名称
蒋经理 来源于 标准通讯录中 黎明液压有限公司对应的联系人,如果标准通讯录中未找到该供应商名称,那么联系人、电话、传真均空白,其他工作继续进行
3775893399 来源于 标准通讯录中蒋经理对应电话
7732760 来源于 标准通讯录中蒋经理对应传真,如没有就空着
过滤器 来源于 索赔明细中 索赔产品
13 来源于 索赔明细中 数量
2016年8月 来源于 索赔明细中 发生月份
230 来源于 索赔明细中 原始金额
269.1 来源于 索赔明细中 索赔单金额
希望将每一行转化为索赔单之后,另存为excel表,保存在桌面上“索赔单”文件夹,且表格的名字命名方式为:索赔单编号后六位数字+供应商名称,本例子最后生成的excel名称应为“090476黎明液压有限公司”。
作者:
yayahzmeng
时间:
2017-1-20 02:11
请测试一下!
3分半,生成200份索赔单,个人觉得不太满意
Sub autoT()
Application.ScreenUpdating = False
Dim arr, brr, Mxi, Tongx, t, i&, j%, k%, a, b, facN
Dim wb As Workbook, wk As Workbook
Dim sh As Worksheet, ipath
t = Timer
ipath = ThisWorkbook.Path & "\索赔单"
Set Mxi = CreateObject("scripting.dictionary")
Set Tongx = CreateObject("scripting.dictionary")
arr = Array("J4", "D5", "C17", "A17", "c4", "D8", "J8", "H10", "D10", "J10", "I21")
brr = Array("I6", "J6", "I7")
'清空标准表单
For i = 0 To UBound(arr)
Range(arr(i)) = ""
Next
For i = 0 To UBound(brr)
Range(brr(i)) = ""
Next
Set wb = Workbooks.Open(ThisWorkbook.Path & "\索赔明细.xlsx")
' wb.Sheet1.Activate
With ActiveSheet
a = .Range("A2:I" & .[a1].End(4).Row)
For i = 1 To UBound(a)
For j = 2 To 9
Mxi(a(i, 1)) = Mxi(a(i, 1)) & a(i, j) & vbTab
Next
Next
End With
Erase a: wb.Close False: i = 1
Set wb = Nothing
'
' '按Mxi序号批量建立sheets
' For i = 1 To Mxi.Count
' If TypeName(Application.Evaluate("(" & i & ")!A1")) = "Error" Then
' Sheets("标准").Copy after:=Worksheets(Worksheets.Count)
' With ActiveSheet
' .Name = "(" & i & ")"
' End With
' End If
' Next
Set wb = Workbooks.Open(ThisWorkbook.Path & "\标准通讯录.xls")
With ActiveSheet
a = .Range("D2:G" & .[d2].End(4).Row)
For i = 1 To UBound(a)
Tongx(a(i, 1)) = a(i, 2) & vbTab & a(i, 3) & vbTab & a(i, 4)
Next
End With
Erase a: wb.Close False
Set wb = Nothing
For i = 1 To Mxi.Count
a = Split(Mxi(i), vbTab)
With ActiveSheet
For k = 0 To UBound(arr) Step 1
If k <= 7 Then
.Range(arr(k)) = a(k)
End If
If k = 8 Then .Range(arr(k)) = a(5)
If k = 9 Then .Range(arr(k)) = a(6)
If k = 10 Then .Range(arr(k)) = a(2)
Next
formN = Right(a(4), 6)
Erase a
facN = .Range(arr(1))
b = Split(Tongx(facN), vbTab)
For j = 0 To UBound(brr)
.Range(brr(j)) = "" & b(j)
Next
Erase b
fname = formN & facN
Set wk = Workbooks.Add
.Copy before:=wk.Worksheets("sheet1")
For Each sh In wk.Worksheets
If sh.Name Like "*Sheet*" Then
Application.DisplayAlerts = False
sh.Delete
Application.DisplayAlerts = True
End If
Next sh
wk.SaveAs ipath & fname & ".xls"
wk.Close
End With
Next
Application.ScreenUpdating = True
MsgBox "成功生成 " & Mxi.Count & " 份索赔单,耗时 " & Format(Timer - t, "0.000") & " 秒!"
End Sub
复制代码
作者:
yayahzmeng
时间:
2017-1-20 09:12
本帖最后由 yayahzmeng 于 2017-1-20 15:30 编辑
昨晚居然没有发成功,请测试一下
Sub autoT()
Application.ScreenUpdating = False
Dim arr, brr, Mxi, Tongx, t, i&, j%, k%, a, b, facN
Dim wb As Workbook, wk As Workbook
Dim sh As Worksheet, ipath
t = Timer
ipath = ThisWorkbook.Path & "\索赔单"
Set Mxi = CreateObject("scripting.dictionary")
Set Tongx = CreateObject("scripting.dictionary")
arr = Array("J4", "D5", "C17", "A17", "c4", "D8", "J8", "H10", "D10", "J10", "I21")
brr = Array("I6", "J6", "I7")
'清空标准表单
For i = 0 To UBound(arr)
Range(arr(i)) = ""
Next
For i = 0 To UBound(brr)
Range(brr(i)) = ""
Next
Set wb = Workbooks.Open(ThisWorkbook.Path & "\索赔明细.xlsx")
With ActiveSheet
a = .Range("A2:I" & .[a1].End(4).Row)
For i = 1 To UBound(a)
For j = 2 To 9
Mxi(a(i, 1)) = Mxi(a(i, 1)) & a(i, j) & vbTab
Next
Next
End With
Erase a: wb.Close False: i = 1
Set wb = Nothing
Set wb = Workbooks.Open(ThisWorkbook.Path & "\标准通讯录.xls")
With ActiveSheet
a = .Range("D2:G" & .[d2].End(4).Row)
For i = 1 To UBound(a)
Tongx(a(i, 1)) = a(i, 2) & vbTab & a(i, 3) & vbTab & a(i, 4)
Next
End With
Erase a: wb.Close False
Set wb = Nothing
For i = 1 To Mxi.Count
a = Split(Mxi(i), vbTab)
With ActiveSheet
For k = 0 To UBound(arr) Step 1
If k <= 7 Then
.Range(arr(k)) = a(k)
End If
If k = 8 Then .Range(arr(k)) = a(5)
If k = 9 Then .Range(arr(k)) = a(6)
If k = 10 Then .Range(arr(k)) = a(2)
Next
formN = Right(a(4), 6)
Erase a
facN = .Range(arr(1))
b = Split(Tongx(facN), vbTab)
For j = 0 To UBound(brr)
.Range(brr(j)) = "" & b(j)
Next
Erase b
fname = formN & facN
Set wk = Workbooks.Add
.Copy before:=wk.Worksheets("sheet1")
For Each sh In wk.Worksheets
If sh.Name Like "*Sheet*" Then
Application.DisplayAlerts = False
sh.Delete
Application.DisplayAlerts = True
End If
Next sh
wk.SaveAs ipath & fname & ".xls"
wk.Close
End With
Next
Application.ScreenUpdating = True
MsgBox "成功生成 " & Mxi.Count & " 份索赔单,耗时 " & Format(Timer - t, "0.000") & " 秒!"
End Sub
复制代码
打开“标准表格.xlsm”,ctrl + p
测试了一下,900秒生成4000份,个人觉得速度不太令人满意,不知道其它大神有没有更快的保存工作簿的代码
欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/)
Powered by Discuz! X3.3