VBA海量数据汇总
我已经搜索和做这项任务好几个星期了,但从未找到解决方案。 我希望你们能给我一个解决方案。VBA海量数据汇总,vba,excel,Vba,Excel,我已经搜索和做这项任务好几个星期了,但从未找到解决方案。 我希望你们能给我一个解决方案。 我的情况如下: 1.我有两本excel工作簿 (第一:Log.xlsx-汇总数据, 第二:data.xls-数据源) 2.在log.xlsx中,我有生产日期、产品id和批次 3.在data.xls中,我有生产产量结果(不同id的工作表不同) 仅供参考,工作表编号约为100张工作表 4.我想搜索同一日期、产品id和批次的产量结果(data.xls)(与Log.xlsx中的匹配),然后在Log.xlsx中显示产
我的情况如下:
1.我有两本excel工作簿
(第一:Log.xlsx-汇总数据,
第二:data.xls-数据源)
2.在log.xlsx中,我有生产日期、产品id和批次
3.在data.xls中,我有生产产量结果(不同id的工作表不同)
仅供参考,工作表编号约为100张工作表
4.我想搜索同一日期、产品id和批次的产量结果(data.xls)(与Log.xlsx中的
匹配),然后在Log.xlsx中显示产量结果
5.data.xls工作表名称与产品id不同,但实际产品id显示在单元格B2中
你能告诉我如何使这成为可能吗?
非常感谢您的帮助。
谢谢。
假设代码在log.xlsx中,并且两个工作簿当前都处于打开状态,这将把第4列到第7列从data.xls复制到log.xlsx
Option Explicit
Public Sub DataSummary()
Dim wbData As Workbook
Dim wsLog As Worksheet
Dim wsDataSet As Worksheet
Dim i, j, k As Integer
Dim prodDate As Date
Dim prodID As String
Dim lotNumber As Integer
Set wbData = Workbooks("Data.xls")
Set wsLog = ThisWorkbook.ActiveSheet
For i = 2 To wsLog.UsedRange.Rows.Count
prodDate = wsLog.Cells(i, 1).Value
prodID = wsLog.Cells(i, 2).Value
lotNumber = wsLog.Cells(i, 3).Value
For Each wsDataSet In wbData.Worksheets
If wsDataSet.Cells(2, 2).Value = prodID Then
For j = 4 To wsDataSet.UsedRange.Rows.Count
If wsDataSet.Cells(j, 1).Value = prodDate And wsDataSet.Cells(j, 3).Value = lotNumber Then
wsLog.Cells(i, 4).Value = wsDataSet.Cells(j, 4).Value
wsLog.Cells(i, 5).Value = wsDataSet.Cells(j, 5).Value
wsLog.Cells(i, 6).Value = wsDataSet.Cells(j, 6).Value
wsLog.Cells(i, 7).Value = wsDataSet.Cells(j, 7).Value
Exit For
End If
Next j
Exit For
End If
Next wsDataSet
Next i
End Sub
下面是带有附加检查的代码。当没有匹配的yeild时,它会放入“0”
Option Explicit
Public Sub DataSummary()
Dim wbData As Workbook
Dim wsLog As Worksheet
Dim wsDataSet As Worksheet
Dim i, j, k As Integer
Dim prodDate As Date
Dim prodID As String
Dim lotNumber As Integer
Dim foundIt As Boolean
Dim Message As String
Message = "0"
Set wbData = Workbooks("Data.xls")
Set wsLog = ThisWorkbook.ActiveSheet
For i = 2 To wsLog.UsedRange.Rows.Count
foundIt = False
prodDate = wsLog.Cells(i, 1).Value
prodID = wsLog.Cells(i, 2).Value
lotNumber = wsLog.Cells(i, 3).Value
For Each wsDataSet In wbData.Worksheets
If wsDataSet.Cells(2, 2).Value = prodID Then
For j = 4 To wsDataSet.UsedRange.Rows.Count
If wsDataSet.Cells(j, 1).Value = prodDate And wsDataSet.Cells(j, 3).Value = lotNumber Then
wsLog.Cells(i, 4).Value = wsDataSet.Cells(j, 4).Value
wsLog.Cells(i, 5).Value = wsDataSet.Cells(j, 5).Value
wsLog.Cells(i, 6).Value = wsDataSet.Cells(j, 6).Value
wsLog.Cells(i, 7).Value = wsDataSet.Cells(j, 7).Value
foundIt = True
Exit For
End If
Next j
Exit For
End If
Next wsDataSet
If Not foundIt Then
wsLog.Cells(i, 4).Value = Message
wsLog.Cells(i, 5).Value = Message
wsLog.Cells(i, 6).Value = Message
wsLog.Cells(i, 7).Value = Message
End If
Next i
End Sub
请记住,这只是实际解决方案的粗略部分,您需要添加异常处理、更多检查以查看数据是否正确等。非常感谢!!最后我可以总结数据了!正如专家们所期望的那样。再次谢谢你!。我能再问一个问题吗?对于没有产量/未识别批号的批号,我应该添加什么来显示“0”?提前谢谢你!!