20170612xlVBA多文件多类别分类求和匹配
Public Sub Basic_CodeFrame()
AppSettings
'On Error GoTo ErrHandler
Dim StartTime, UsedTime As Variant
StartTime = VBA.Timer
'Input code here Dim Wb As Workbook
Dim Sht As Worksheet
Dim OpenWb As Workbook
Dim OpenSht As Worksheet
Dim NewWb As Workbook
Dim NewSht As Worksheet
Dim Arr As Variant
Dim i As Long, j As Long
Dim EndRow As Long
Dim Brr()
Dim Crr()
Dim Drr()
Dim Index As Long
Dim Index1 As Long
Dim Index2 As Long
Dim OneKey As Variant Dim Title As Variant Dim FolderPath As String
Const FolderName As String = "原始文件"
Const OutPutName As String = "结果文件" Const OpFile1 As String = "台面补货d.xlsx"
Const OpFile2 As String = "品牌补货d.xlsx"
Const OpFile3 As String = "小类补货d.xlsx" Dim OpPath As String Const AName As String = "盘点"
Dim aFile As String, aPath As String
Const CName As String = "产品资料"
Dim cFile As String, cPath As String
Const BName As String = "库存"
Dim bFile As String, bPath As String
Const DName As String = "销售"
Dim dFile As String, dPath As String Dim aInfo(1 To 4) As Object
Dim bInfo(1 To 4) As Object
Dim cInfo(1 To 18) As Object
Dim dInfo(1 To 5) As Object
Dim dCate As Object '小类
Dim dBrand As Object '品牌
Dim Cate As String
Dim Brand As String
Set dCate = CreateObject("Scripting.Dictionary")
Set dBrand = CreateObject("Scripting.Dictionary") Set Wb = Application.ThisWorkbook
Set Sht = Wb.Worksheets("标题")
Title = Sht.Range("A1:X1").Value
FolderPath = Wb.Path & Application.PathSeparator & _
FolderName & Application.PathSeparator '先到C表保存各种字段信息 For j = 1 To 18
Set cInfo(j) = CreateObject("Scripting.Dictionary")
Next j cFile = Dir(FolderPath & "*" & CName & "*.xls*")
cPath = FolderPath & cFile
Debug.Print cPath Set OpenWb = Application.Workbooks.Open(cPath)
Set OpenSht = OpenWb.Worksheets(1)
With OpenSht
EndRow = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row
Set Rng = .Range("A2:R" & EndRow)
Arr = Rng.Value
For i = LBound(Arr) To UBound(Arr)
Key = CStr(Arr(i, 1))
Key = Replace(Key, " ", "")
For j = LBound(Arr, 2) To UBound(Arr, 2)
cInfo(j)(Key) = Arr(i, j)
Next j
Next i
End With
Set OpenSht = Nothing
OpenWb.Close False '再到A表读取报货单
For j = 1 To 4
Set aInfo(j) = CreateObject("Scripting.Dictionary")
Next j aFile = Dir(FolderPath & "*" & AName & "*.xls*")
aPath = FolderPath & aFile
Debug.Print aPath Set OpenWb = Application.Workbooks.Open(aPath)
Set OpenSht = OpenWb.Worksheets(1) With OpenSht
EndRow = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row
Set Rng = .Range("A2:D" & EndRow)
Arr = Rng.Value For i = LBound(Arr) To UBound(Arr)
Key = CStr(Arr(i, 1))
Key = Replace(Key, " ", "")
For j = LBound(Arr, 2) To UBound(Arr, 2)
aInfo(j)(Key) = Arr(i, j)
Next j
Next i End With
Set OpenSht = Nothing
OpenWb.Close False '再到B表读取库存
For j = 1 To 4
Set bInfo(j) = CreateObject("Scripting.Dictionary")
Next j bFile = Dir(FolderPath & "*" & BName & "*.xls*")
bPath = FolderPath & bFile
Debug.Print bPath Set OpenWb = Application.Workbooks.Open(bPath)
Set OpenSht = OpenWb.Worksheets(1) With OpenSht
EndRow = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row
Set Rng = .Range("A2:D" & EndRow)
Arr = Rng.Value For i = LBound(Arr) To UBound(Arr)
Key = CStr(Arr(i, 1))
Key = Replace(Key, " ", "")
For j = LBound(Arr, 2) To UBound(Arr, 2)
bInfo(j)(Key) = Arr(i, j)
Next j
Next i End With
Set OpenSht = Nothing
OpenWb.Close False '再到D表读取销售
For j = 1 To 5
Set dInfo(j) = CreateObject("Scripting.Dictionary")
Next j dFile = Dir(FolderPath & "*" & DName & "*.xls*")
dPath = FolderPath & dFile
Debug.Print dPath Set OpenWb = Application.Workbooks.Open(dPath)
Set OpenSht = OpenWb.Worksheets(1) With OpenSht
EndRow = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row
Set Rng = .Range("A2:D" & EndRow)
Arr = Rng.Value For i = LBound(Arr) To UBound(Arr)
Key = CStr(Arr(i, 1))
Key = Replace(Key, " ", "")
For j = LBound(Arr, 2) To UBound(Arr, 2)
dInfo(j)(Key) = Arr(i, j)
Next j
Next i End With
Set OpenSht = Nothing
OpenWb.Close False '保存上报品牌与小类
'For Each OneKey In aInfo(1).keys
'Brand = cInfo(6)(OneKey) '保存品牌
'dBrand(Brand) = ""
'Cate = cInfo(4)(OneKey) '保存小类
'dCate(Cate) = ""
'Next OneKey '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> '计算台面补货
ReDim Brr(1 To 24, 1 To 1)
Index = 0
For Each OneKey In aInfo(1).keys
Index = Index + 1
ReDim Preserve Brr(1 To 24, 1 To Index)
Brr(1, Index) = OneKey & " " '条码
Brr(2, Index) = cInfo(2)(OneKey) '商品名称2
Brr(3, Index) = IIf(aInfo(4)(OneKey) = "", 0, aInfo(4)(OneKey)) '商场库存4
Brr(4, Index) = IIf(bInfo(3)(OneKey) = "", 0, bInfo(3)(OneKey)) '总部库存3
Brr(5, Index) = IIf(dInfo(3)(OneKey) = "", 0, dInfo(3)(OneKey)) '销售数量3
Brr(6, Index) = cInfo(6)(OneKey) '品牌6
Brr(7, Index) = cInfo(4)(OneKey) '小类4 Brand = cInfo(6)(OneKey) '保存品牌
dBrand(Brand) = ""
Cate = cInfo(4)(OneKey) '保存小类
dCate(Cate) = "" Brr(8, Index) = (Brr(5, Index) - Brr(3, Index)) * 1.5 '(D-A)*1.5 要出多少货
If Brr(8, Index) > 0 Then
If Brr(4, Index) >= Brr(8, Index) Then '库存足够出货
Brr(9, Index) = Brr(8, Index) '直接出货
Brr(10, Index) = "" '无需采购
Else
Brr(9, Index) = Brr(4, Index) '库存全出
Brr(10, Index) = Brr(8, Index) - Brr(4, Index) '计算采购
End If
End If
'------
Brr(11, Index) = cInfo(3)(OneKey) '大类
Brr(12, Index) = cInfo(5)(OneKey) '规格
For j = 1 To 12
Brr(j + 12, Index) = cInfo(j + 6)(OneKey)
Next j
Next OneKey '创建台面补货文件
OpPath = Wb.Path & "\" & OutPutName & "\" & Replace(OpFile1, "d", "-" & Split(dFile, ".")(0))
Debug.Print OpPath Set NewWb = Application.Workbooks.Add()
Set NewSht = NewWb.Worksheets(1)
NewSht.Name = Split(OpFile1, "d")(0)
NewWb.SaveAs OpPath
With NewSht
.Columns("A:A").NumberFormat = "@"
.Range("A1:X1").Value = Title
.Range("a2").Resize(Index, 24).Value = _
Application.WorksheetFunction.Transpose(Brr)
End With NewWb.Close True
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> '计算品牌与小类补货
ReDim Crr(1 To 24, 1 To 1)
ReDim Drr(1 To 24, 1 To 1) Index1 = 0
Index2 = 0
For Each OneKey In cInfo(1).keys Brand = cInfo(6)(OneKey) '保存品牌
If dBrand.Exists(Brand) Then '属于改品牌
Index1 = Index1 + 1
ReDim Preserve Crr(1 To 24, 1 To Index1)
Crr(1, Index1) = OneKey & " " '条码
Crr(2, Index1) = cInfo(2)(OneKey) '商品名称2
Crr(3, Index1) = IIf(aInfo(4)(OneKey) = "", 0, aInfo(4)(OneKey)) '商场库存4
Crr(4, Index1) = IIf(bInfo(3)(OneKey) = "", 0, bInfo(3)(OneKey)) '总部库存3
Crr(5, Index1) = IIf(dInfo(3)(OneKey) = "", 0, dInfo(3)(OneKey)) '销售数量3
Crr(6, Index1) = cInfo(6)(OneKey) '品牌6
Crr(7, Index1) = cInfo(4)(OneKey) '小类4
Crr(8, Index1) = (Crr(5, Index1) - Crr(3, Index1)) * 1.5 '(D-A)*1.5 要出多少货
If Crr(8, Index1) > 0 Then
If Crr(4, Index1) >= Crr(8, Index1) Then '库存足够出货
Crr(9, Index1) = Crr(8, Index1) '直接出货
Crr(10, Index1) = "" '无需采购
Else
Crr(9, Index1) = Crr(4, Index1) '库存全出
Crr(10, Index1) = Crr(8, Index1) - Crr(4, Index1) '计算采购
End If
End If
'------
Crr(11, Index1) = cInfo(3)(OneKey) '大类
Crr(12, Index1) = cInfo(5)(OneKey) '规格
For j = 1 To 12
Crr(j + 12, Index1) = cInfo(j + 6)(OneKey)
Next j
End If
Cate = cInfo(4)(OneKey) '保存小类
If dCate.Exists(Cate) Then
Index2 = Index2 + 1
ReDim Preserve Drr(1 To 24, 1 To Index2)
Drr(1, Index2) = OneKey & " " '条码
Drr(2, Index2) = cInfo(2)(OneKey) '商品名称2
Drr(3, Index2) = IIf(aInfo(4)(OneKey) = "", 0, aInfo(4)(OneKey)) '商场库存4
Drr(4, Index2) = IIf(bInfo(3)(OneKey) = "", 0, bInfo(3)(OneKey)) '总部库存3
Drr(5, Index2) = IIf(dInfo(3)(OneKey) = "", 0, dInfo(3)(OneKey)) '销售数量3
Drr(6, Index2) = cInfo(6)(OneKey) '品牌6
Drr(7, Index2) = cInfo(4)(OneKey) '小类4
Drr(8, Index2) = (Drr(5, Index2) - Drr(3, Index2)) * 1.5 '(D-A)*1.5 要出多少货
If Drr(8, Index2) > 0 Then
If Drr(4, Index2) >= Drr(8, Index2) Then '库存足够出货
Drr(9, Index2) = Drr(8, Index2) '直接出货
Drr(10, Index2) = "" '无需采购
Else
Drr(9, Index2) = Drr(4, Index2) '库存全出
Drr(10, Index2) = Drr(8, Index2) - Drr(4, Index2) '计算采购
End If
End If
'------
Drr(11, Index2) = cInfo(3)(OneKey) '大类
Drr(12, Index2) = cInfo(5)(OneKey) '规格
For j = 1 To 12
Drr(j + 12, Index2) = cInfo(j + 6)(OneKey)
Next j
End If Next OneKey '创建品牌补货文件
OpPath = Wb.Path & "\" & OutPutName & "\" & Replace(OpFile2, "d", "-" & Split(dFile, ".")(0))
Debug.Print OpPath Set NewWb = Application.Workbooks.Add()
Set NewSht = NewWb.Worksheets(1)
NewSht.Name = Split(OpFile2, "d")(0)
NewWb.SaveAs OpPath
With NewSht
.Columns("A:A").NumberFormat = "@"
.Range("A1:X1").Value = Title
.Range("a2").Resize(Index, 24).Value = _
Application.WorksheetFunction.Transpose(Crr)
End With NewWb.Close True '创建小类补货文件
OpPath = Wb.Path & "\" & OutPutName & "\" & Replace(OpFile3, "d", "-" & Split(dFile, ".")(0))
Debug.Print OpPath Set NewWb = Application.Workbooks.Add()
Set NewSht = NewWb.Worksheets(1)
NewSht.Name = Split(OpFile3, "d")(0)
NewWb.SaveAs OpPath
With NewSht
.Columns("A:A").NumberFormat = "@"
.Range("A1:X1").Value = Title
.Range("a2").Resize(Index, 24).Value = _
Application.WorksheetFunction.Transpose(Drr)
End With NewWb.Close True UsedTime = VBA.Timer - StartTime
'Debug.Print "UsedTime:" & Format(UsedTime, "0.000 Seconds")
MsgBox "UsedTime:" & Format(UsedTime, "0.000 Seconds"), vbOKOnly, "NS QQ "
ErrorExit:
AppSettings False
Exit Sub
ErrHandler:
If Err.Number <> 0 Then
MsgBox Err.Description & "!", vbCritical, "NS QQ "
Debug.Print Err.Description
Err.Clear
Resume ErrorExit
End If
End Sub
Public Sub AppSettings(Optional IsStart As Boolean = True)
If IsStart Then
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Application.StatusBar = ">>>>>>>>Macro Is Running>>>>>>>>"
Else
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.StatusBar = False
End If
End Sub
20170612xlVBA多文件多类别分类求和匹配的更多相关文章
- 关于ios object-c 类别-分类 category 的静态方法与私有变量,协议 protocol
关于ios object-c 类别-分类 category 的静态方法与私有变量,协议 protocol 2014-02-18 19:57 315人阅读 评论(0) 收藏 举报 1.category, ...
- Atitit 基于图片图像 与文档混合文件夹的分类
Atitit 基于图片图像 与文档混合文件夹的分类 太小的文档(txt doc csv exl ppt pptx)单独分类 Mov10KminiDoc 但是可能会有一些书法图片迁移,因为他们很微小,需 ...
- Linux下find一次查找多个指定类型文件,指定文件或者排除某类文件,在 GREP 中匹配多个关键 批量修改文件名等
http://blog.sina.com.cn/s/blog_62e7fe670101dg9d.html linux下二进制文件查找: strings 0000.ts | grep -o " ...
- [DeeplearningAI笔记]Multi-class classification多类别分类Softmax regression_02_3.8-3.9
Multi-class classification多类别分类 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.8 Softmax regression 原有课程我们主要介绍的是二分分类( ...
- Excel技巧--分类求和与空白批量填充
分类求和: 当我们要对以上多个分类空白求和时,可以巧用Alt+=键: 1.选择对应要求和的列: 2.点击“查找与选择”下拉列,选择“定位条件”,对话框选择“空值”,点确定.将列中的空白单元格选好: 3 ...
- OC的类别(分类)和拓展
一.分类: 1.适用范围 当你已经封装好了一个类(也可能是系统类.第三方库),不想在改动这个类了,可是随着程序功能的增加需要在类中增加一个方法,这时我们不必修改主类,只需要给你原来的类增加一 ...
- shell脚本(傻瓜式处理文件到指定分类)
前言 每一到两周,我大概会新增十多个甚至更多的资料文件,都是些最近遇到的一些问题的总结或者相关技术文档,但是资料都是在公司电脑上,拷贝到自己电脑上后,又得一个个去找一个这个应该放到哪个分类,个人感觉很 ...
- Codeigniter文件上传类型不匹配错误
Codeigniter的文件上传类方便了我们使用PHP来处理文件上传的操作,使用起来非常简单,如下: $config['upload_path'] = './uploads/'; $config[ ...
- IE9以上 CSS文件因Mime类型不匹配而被忽略 其他浏览器及IE8以下显示正常
什么是Mime类型? MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名 ...
随机推荐
- Jackson基础
一.所需jar包: jackson-core-x.x.x-rc4.jar.jackson-databind-x.x.x-rc4.jar.jackson-annotations-x.x.x-rc4.ja ...
- Java EE企业应用发展
新形式下的企业应用特点企业应用系统从封闭走向开放,由局域网转到互联网,随着涉众面的极大扩展,新的企业应用要求多浏览器支持,国际化支持,全球业务的互联互通.企业需求提升.除了功能性需求,客户对于安全,性 ...
- 编译安装 http
1. 安装 apr http服务依赖 apr和apr-util ,安装 http 前需先安装这两个程序 apr 简介:http://www.cnblogs.com/Alight/p/3997777.h ...
- MySQL中INSERT的一般用法
原文链接:http://www.blogjava.net/midnightPigMan/archive/2014/12/15/421406.html MySQL中INSERT的一般用法 INSERT语 ...
- 20145208 蔡野《网络对抗》shellcode注入&Return-to-libc攻击深入
20145208 蔡野<网络对抗>shellcode注入&Return-to-libc攻击深入 Shellcode注入 shellcode的获取代码 我使用了许心远同学博客中的代码 ...
- STM32各个文件介绍、uCOSII文件介绍
(1)core_cm3.c , core_cm.h:获取设置CM3内核,配置一些内核寄存器,用到CM3核的都需要: (2)stm32f10x.h 和 system_stm32f10x.c , syst ...
- 我们能从 jQuery 的一个正则表达式中学到什么?
注意,该篇文章当前为粗糙的 v0.9 版本,会在稍后润色更新. 让我们来看一道思考题,根据 rejectExp,分析其正则执行过程中如何进行过滤?包含哪些执行步骤? rejectExp 变量的值如下: ...
- C#中基于流的XML文件操作笔记
System.Xml.XmlReader和System.Xml.XmlWriters是两个抽象类,XmlReader提供了对于XML数据的快速,非缓存,只进模式的读取器,XmlWriter表示一个编写 ...
- cogs 1962. [HAOI2015]树上染色
★★☆ 输入文件:haoi2015_t1.in 输出文件:haoi2015_t1.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] 有一棵点数为N的树,树边有边 ...
- Spring Cloud微服务体系搭建
前期架构设计图: 参考博文: Eureka相关: Eureka注册与发现(高可用注册中心.注册服务.Feign服务调用):https://blog.csdn.net/qq_32529383/artic ...