发布网友 发布时间:2022-05-02 04:23
共1个回答
热心网友 时间:2022-06-28 04:41
Sub 提取()
Dim DataWorkbook As Workbook '库存单.xls工作簿
Dim DataSheet As Worksheet, DataSheetName As String 'DataSheet=当前操作的工作表,DataSheetName=当前操作工作表的表名
Dim HuizongSheet As Worksheet '汇总单工作表
Dim GoodCount As Long
Dim GoodArr, GoodArrB()
Dim i As Long, j As Long
Application.ScreenUpdating = False
On Error Resume Next
Set DataWorkbook = GetObject(ThisWorkbook.Path & "\库存单.xls")
If Err <> 0 Then
MsgBox "没有找到 库存单.xls ,请把 库存单.xls 与 汇总.xls 放在同一目录下再运行此程序。", vbExclamation
Exit Sub
End If
Set HuizongSheet = Worksheets("汇总单")
GoodCount = 0 '产品计数归零
For Each DataSheet In DataWorkbook.Sheets '遍历每个库存单工作表
DataSheet.Activate
With DataSheet
GoodArr = .Range([c2], .[h65536].End(3)) '把库存数据的 C-H列 装进数组
'GoodArr(1,n) n= 1:C产品名称 2:D空 3:E进货数量 4:F总数 5:G进货次数 6:H进货时间
For i = 1 To UBound(GoodArr)
If Len(GoodArr(i, 4)) Then '总数不为空时,认定找到一个产品
GoodCount = GoodCount + 1 '产品数加1
ReDim Preserve GoodArrB(1 To 2, 1 To GoodCount)
GoodArrB(1, GoodCount) = DataSheet.Name & "-" & GoodArr(i, 1) '产品名称
GoodArrB(2, GoodCount) = "进货时间: " & GoodArr(i, 6) '进货时间
ElseIf Len(GoodArr(i, 6)) Then '总数为空,且进货时间不为空
GoodArrB(2, GoodCount) = GoodArrB(2, GoodCount) & " " & GoodArr(i, 6) '累加进货时间
End If
Next
End With
Next
DataWorkbook.Close '关闭打开的 库存单.xls
HuizongSheet.[a1].Resize(GoodCount, 2) = Application.Transpose(GoodArrB) '把提取出的数据显示在汇总单工作表上
Application.ScreenUpdating = True
End Sub