EXCEL VBA 多个表格的处理和操作汇总

EXCEL VBA 多个表格的处理和操作汇总

python 复制代码
Sub 需求1()
    fpath = ThisWorkbook.Path & "\"
    

    Dim wbdian As Workbook
    Set wbdian = Workbooks.Open(fpath & "闪电退税返点比例-zxh更新.xls")
    Dim wb As Worksheet
    Set wb = wbdian.Worksheets(1)
    Dim dicdian As Object
    Set dicdian = CreateObject("scripting.dictionary")
    For i = 2 To wb.Range("a" & wb.Cells.Rows.Count).End(xlUp).Row
        k = wb.Cells(i, "e").Value
        panduan = CDate(Right(wb.Cells(i, "l"), Len(wb.Cells(i, "l")) - InStr(1, wb.Cells(i, "l"), "-")))
        If Now < panduan Then
            If Not dicdian.exists(k) Then
                kitem = wb.Cells(i, "k")
                dicdian.Add k, kitem
            End If
        End If
    Next
    wbdian.Close
    

    Dim wzx As Worksheet
    Set wzx = ThisWorkbook.Worksheets("渠道物流返利明细表")
    wzx.Range("a3:i" & wzx.Cells.Rows.Count).Clear
    Dim wbk As Workbook
    Set wbk = Workbooks.Open(fpath & "2024年意大利flash公司库存-2024.3.18.xlsx")
    Dim dic As Object
    Set dic = CreateObject("scripting.dictionary")
    Dim dicdate As Object
    Set dicdate = CreateObject("scripting.dictionary")
    Dim wk As Worksheet
    Set wk = wbk.Worksheets(1)
    wkendrow = wk.Range("a" & wk.Cells.Rows.Count).End(xlUp).Row
    For i = 3 To wkendrow
        If wk.Cells(i, "r") <> "" And Left(wk.Cells(i, "r"), 6) <> wk.Cells(i, 2) Then
            k1 = wk.Cells(i, 2)
            k2 = wk.Cells(i, "o")
            k3 = wk.Cells(i, "r")
            kitem = wk.Cells(i, "M").Value
            kdate = wk.Cells(i, "p")
            
            If Not dicdate.exists(k2) Then
                dicdate.Add k2, kdate
            End If
            
            k = k1 & "-" & k2 & "-" & k3
            If Not dic.exists(k) Then
                dic.Add k, kitem
            Else
                dic(k) = dic(k) + kitem
            End If
        End If
    Next
    
    wbk.Close
    
    
    
    kdicarr = dic.keys()
    kdicbrr = dic.items()
    wzxrow = 3
    For i = 0 To UBound(kdicarr)
        crr = Split(kdicarr(i), "-")
        wzx.Cells(wzxrow, 1) = i + 1
        wzx.Cells(wzxrow, 2) = crr(2)
        wzx.Cells(wzxrow, 3) = crr(0)
        wzx.Cells(wzxrow, 5) = crr(1)
        wzx.Cells(wzxrow, 6) = kdicbrr(i)
        wzx.Cells(wzxrow, 4) = dicdate(crr(1))
        wzx.Cells(wzxrow, 7) = dicdian(crr(2))

        If Month(wzx.Cells(wzxrow, 4)) >= 1 And Month(wzx.Cells(wzxrow, 4)) <= 3 Then
            wzx.Cells(wzxrow, 9) = Year(wzx.Cells(wzxrow, 4)) & "年第" & 1 & "季度"
        ElseIf Month(wzx.Cells(wzxrow, 4)) >= 4 And Month(wzx.Cells(wzxrow, 4)) <= 6 Then
            wzx.Cells(wzxrow, 9) = Year(wzx.Cells(wzxrow, 4)) & "年第" & 2 & "季度"
        ElseIf Month(wzx.Cells(wzxrow, 4)) >= 7 And Month(wzx.Cells(wzxrow, 4)) <= 9 Then
            wzx.Cells(wzxrow, 9) = Year(wzx.Cells(wzxrow, 4)) & "年第" & 3 & "季度"
        Else
            wzx.Cells(wzxrow, 9) = Year(wzx.Cells(wzxrow, 4)) & "年第" & 4 & "季度"
        End If
        wzx.Cells(wzxrow, 8).FormulaR1C1 = "=RC[-2]*RC[-1]"
        wzx.Cells(wzxrow, 8).NumberFormatLocal = "#,##0.00 ""€"";-#,##0.00 ""€"""
        
        
        wzxrow = wzxrow + 1
    Next

    wzx.Cells(wzxrow, 1) = "合计"
    wzx.Cells(wzxrow, "f") = Application.WorksheetFunction.Sum(wzx.Range("f3:f" & wzxrow - 1))
    wzx.Cells(wzxrow, "h") = Application.WorksheetFunction.Sum(wzx.Range("h3:h" & wzxrow - 1))
    wzx.Cells(wzxrow, "f").NumberFormatLocal = "#,##0.00 ""€"";-#,##0.00 ""€"""
    wzx.Cells(wzxrow, "h").NumberFormatLocal = "#,##0.00 ""€"";-#,##0.00 ""€"""
    
End Sub

Sub 拆分()
    Dim dic As Object
    Set dic = CreateObject("scripting.dictionary")
    Dim wzx As Worksheet
    Set wzx = ThisWorkbook.Worksheets("渠道物流返利明细表")
    Dim wf As Worksheet
    
    For i = 3 To wzx.Range("a" & wzx.Cells.Rows.Count).End(xlUp).Row - 1
        kdaima = wzx.Cells(i, 2)
        If Not dic.exists(kdaima) Then
            dic.Add kdaima, ""
            ThisWorkbook.Worksheets("xxx客户渠道物流返利表模板").Range("a1:i2").Copy
                Sheets.Add After:=ActiveSheet
                Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
                    SkipBlanks:=False, Transpose:=False
                ActiveSheet.Paste
                
                Set wf = ActiveSheet
                wf.Name = kdaima & "客户渠道物流返利表模板"
                wfendrow = wf.Range("a" & wf.Cells.Rows.Count).End(xlUp).Row
                wf.Cells(wfendrow + 1, 1) = 1
                wf.Cells(wfendrow + 1, 2) = wzx.Cells(i, 2)
                wf.Cells(wfendrow + 1, 3) = wzx.Cells(i, 5)
                wf.Cells(wfendrow + 1, 4) = wzx.Cells(i, 4)
                wf.Cells(wfendrow + 1, 5) = wzx.Cells(i, 6)
                wf.Cells(wfendrow + 1, 6) = wzx.Cells(i, 7)
                wf.Cells(wfendrow + 1, 7) = wzx.Cells(i, 8)
                wf.Cells(wfendrow + 1, 8) = wzx.Cells(i, 9)
                wf.Cells(wfendrow + 1, 9) = wzx.Cells(i, 3)
                wf.Cells(1, 1) = kdaima & "-" & Year(wf.Cells(1, 4)) & "年渠道物流返利明细表"
        Else
                Set wf = Worksheets(kdaima & "客户渠道物流返利表模板")
                wfendrow = wf.Range("a" & wf.Cells.Rows.Count).End(xlUp).Row
                wf.Cells(wfendrow + 1, 1) = wf.Cells(wfendrow, 1) + 1
                wf.Cells(wfendrow + 1, 2) = wzx.Cells(i, 2)
                wf.Cells(wfendrow + 1, 3) = wzx.Cells(i, 5)
                wf.Cells(wfendrow + 1, 4) = wzx.Cells(i, 4)
                wf.Cells(wfendrow + 1, 5) = wzx.Cells(i, 6)
                wf.Cells(wfendrow + 1, 6) = wzx.Cells(i, 7)
                wf.Cells(wfendrow + 1, 7) = wzx.Cells(i, 8)
                wf.Cells(wfendrow + 1, 8) = wzx.Cells(i, 9)
                wf.Cells(wfendrow + 1, 9) = wzx.Cells(i, 3)
        End If
    Next
End Sub
相关推荐
唐沢2 分钟前
Unity 读Excel,读取xlsx文件解决方案
unity·excel
余~~1853816280022 分钟前
矩阵碰一碰发视频源码技术解析,支持OEM
数据库·microsoft
蓝天星空34 分钟前
spring cloud gateway 3
java·spring cloud
罗政38 分钟前
PDF书籍《手写调用链监控APM系统-Java版》第9章 插件与链路的结合:Mysql插件实现
java·mysql·pdf
一根稻草君1 小时前
利用poi写一个工具类导出逐级合并的单元格的Excel(通用)
java·excel
kirito学长-Java1 小时前
springboot/ssm网上宠物店系统Java代码编写web宠物用品商城项目
java·spring boot·后端
张声录11 小时前
【ETCD】【实操篇(十五)】etcd集群成员管理:如何高效地添加、删除与更新节点
数据库·etcd
天乐敲代码1 小时前
Etcd静态分布式集群搭建
数据库·分布式·etcd
chengma_0909091 小时前
MySQL 数据库连接数查询、配置
数据库·mysql
木头没有瓜1 小时前
ruoyi 请求参数类型不匹配,参数[giftId]要求类型为:‘java.lang.Long‘,但输入值为:‘orderGiftUnionList
android·java·okhttp