专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 企业信息化

x01.Excel: 共计件数

发布时间:2010-06-06 16:59:05 文章来源:www.iduyao.cn 采编人员:星星草
x01.Excel: 合计件数

由于 VBA 与 Excel 是耦合的,所以还是先看表:

                  

件数的计算,用 Mod 即可。但考虑到要求码洋、数量等多种需求,就该 VBA 登场了。代码如下:

'================================================================
' Count (c) 2014 by x01
'-------------------------
'   计算每行的整件数和码洋
'
' 参数:
'   sheet: 所选的表。
'   numPerBag: 每件的数量。
'   startRow: 开始的行数。
'   endRow: 结束的行数。
'================================================================
Public Sub Count(sheet, numPerBag, startRow, endRow)
    For i = startRow To endRow
        If Application.WorksheetFunction.IsNumber(sheet.Cells(i, 4)) Then
        If Trim(sheet.Cells(i, 1)) <> "合计" Then
        '通过定价选择行数
        'If 15# = sheet.Cells(i, 3) Then
            '计算每行整包
            sheet.Cells(i, 6) = CStr(Int(sheet.Cells(i, 4) / numPerBag)) & "件+" & CStr(sheet.Cells(i, 4) Mod numPerBag)
            
            '计算每行码洋
            sheet.Cells(i, 5) = sheet.Cells(i, 3) * sheet.Cells(i, 4)
        'End If
        End If
        End If
    Next
    Debug.Print CStr(startRow) & " - " & CStr(endRow)
End Sub

'=================================================================
' SumCol (c) 2014 by x01
'-----------------------
'   计算列的合计数,如数量合计,码洋合计等。
'
' 参数:
'   sheet: 所选的表。
'   col: 指定的列。
'   startRow: 开始的行数。
'   endRow: 结束的行数。
'=================================================================
Public Sub SumCol(sheet, col, startRow, endRow)
    Dim result As Double
    result = 0
    For i = startRow To endRow
        If Application.WorksheetFunction.IsNumber(sheet.Cells(i, col)) Then
        If Trim(sheet.Cells(i, 1)) <> "合计" Then
        If 23# = sheet.Cells(i, 3) Then
            result = result + sheet.Cells(i, col)
        End If
        End If
        End If
    Next
    
    Debug.Print CStr(result)
End Sub

在表中,如果有多个发货清单,该宏具有很好的穿透能力,使用起来还是挺方便的。使用方法如下:

1. 仿照 Excel 表的 列 新建一 Excel 表。

2. 按 Atl + F11 快捷键,在显示窗口中插入模块,将上面代码复制到模块中。

3. 在命令窗口中输入类似如下命令并回车即可:

     Count Sheet1,30,1,200

 

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: