Map1e丶 2021-01-18 00:30 采纳率: 100%
浏览 19
已采纳

求大神帮忙翻译下VBA代码是啥意思,零基础刚接触

        
Sub test()
    
    Sheets("打印发货单").Cells.Clear
    Set dNum = CreateObject("scripting.dictionary")
    With Sheets("发货单明细")
        LastRowA = .Cells(Rows.Count, "a").End(xlUp).Row
        For i = 2 To LastRowA
            Key = .Cells(i, 8).Value
            dNum(Key) = ""
        Next
            arr = dNum.keys
            '.Range("a12").Resize(1, UBound(arr) + 1) = arr
            '.Range("a12").Resize(UBound(arr) + 1) = Application.Transpose(arr)
        
        For i = 0 To UBound(arr)
                    Sheets("模板").[A3] = "客户:"
                    Sheets("模板").[c3] = ""
                    Sheets("模板").[f2] = ""
                    Sheets("模板").Range("A5:F14").ClearContents
        
            k = 0
            For j = 2 To LastRowA
                If .Cells(j, 8) = arr(i) Then
                    Sheets("模板").[A3] = "客户:" & .Cells(j, 2)
                    Sheets("模板").[c3] = .Cells(j, 1)
                    Sheets("模板").[f2] = arr(i)
                    
                    Sheets("模板").Cells(5 + k, 1) = .Cells(j, 3)
                    Sheets("模板").Cells(5 + k, 2) = .Cells(j, 4)
                    Sheets("模板").Cells(5 + k, 3) = .Cells(j, 5)
                    Sheets("模板").Cells(5 + k, 4) = .Cells(j, 6)
                    Sheets("模板").Cells(5 + k, 5) = .Cells(j, 7)
                    k = k + 1
                End If
            
            Next
            
            Sheets("模板").Range("A1:g19").Copy
            Sheets("打印发货单").Activate
            Sheets("打印发货单").Cells(i * 19 + 1, 1).Select
            
            
            Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
                SkipBlanks:=False, Transpose:=False
            ActiveSheet.Paste
            
        
        Next
     
            
    End With

End Sub
'1 ,18,35
'1 2 3
 

  • 写回答

3条回答 默认 最新

  • 多米的烦恼 2021-01-19 10:00
    关注
        ' 1. 清空sheet页内容
        Sheets("打印发货单").Cells.Clear
    
        ' 2. 从“发货单明细中拿第八列数据,从第二行开始拿,拿到最后一行”
        Set dNum = CreateObject("scripting.dictionary")
        With Sheets("发货单明细")
            LastRowA = .Cells(Rows.Count, "a").End(xlUp).Row
            For i = 2 To LastRowA
                Key = .Cells(i, 8).Value
                dNum(Key) = ""
            Next
    
        ' 3. 拿完数据保存在arr中
                arr = dNum.keys
                '.Range("a12").Resize(1, UBound(arr) + 1) = arr
                '.Range("a12").Resize(UBound(arr) + 1) = Application.Transpose(arr)
            
        ' 4. 看有多少数据就造多少个客户模板
            For i = 0 To UBound(arr)
        ' 4.1 客户模板信息,A3,C3,F2,以及A5开始到F14的所有位置清空
                        Sheets("模板").[A3] = "客户:"
                        Sheets("模板").[c3] = ""
                        Sheets("模板").[f2] = ""
                        Sheets("模板").Range("A5:F14").ClearContents
    
          ' 4.2 开始构造模板sheet页签。
                k = 0
                For j = 2 To LastRowA
                    If .Cells(j, 8) = arr(i) Then
          ' 从第二行开始,
          ' 取sheet页签“发货单明细”的第二列给A3,第一列给C3,第八列(已保存在arr里面,这里直接取了)给F2。
                        Sheets("模板").[A3] = "客户:" & .Cells(j, 2)
                        Sheets("模板").[c3] = .Cells(j, 1)
                        Sheets("模板").[f2] = arr(i)
                        ' 再挪数据,取sheet页签“发货单明细”的第三到七列,存到模板页签的对应行1到5列
                        Sheets("模板").Cells(5 + k, 1) = .Cells(j, 3)
                        Sheets("模板").Cells(5 + k, 2) = .Cells(j, 4)
                        Sheets("模板").Cells(5 + k, 3) = .Cells(j, 5)
                        Sheets("模板").Cells(5 + k, 4) = .Cells(j, 6)
                        Sheets("模板").Cells(5 + k, 5) = .Cells(j, 7)
                        k = k + 1
                    End If
                
                Next
                
                ' 拷贝模板页签A1到G19的数据,到打印发货单页签对应区域
                Sheets("模板").Range("A1:g19").Copy
                Sheets("打印发货单").Activate
                Sheets("打印发货单").Cells(i * 19 + 1, 1).Select
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案