动态求和

用查询设计器做的手工报表有时候行数是动态,对与行数不确定的情况需要用VBA来写程序实现,写代码还是我的最爱,不管是什么语言。刚刚写完我封装好的代码,觉得实在是牛b。秀下

Option Explicit

'p_queryRows:query起始行位置
'p_queryCol: query起始列位置
'p_conRows:workbook起始行位置
'p_conCol:workbook起始列位置
'p_ContentSheet: 报表的sheet名称
'p_deletePlace:从报表XX处开始检查是否有行项目,不为空则删除
'p_linesToReplace: 要被替换的列数

Sub insert(p_queryRows As Long, p_queryCol As Long, p_conRows As Long, p_conCol As Long, _
            p_ContentSheet As String, p_deletePlace As String, p_linesToReplace As Integer)
     Dim Count As Long                       '复制的列数
    '先将数据全部清除
    '选中要删除的区间C_deltePlace
      With Worksheets(p_ContentSheet).Range(p_deletePlace)
        While Worksheets(p_ContentSheet).Range(p_deletePlace).Value <> ""   '根据空行判断是否删除
            '选中删除
            Worksheets(p_ContentSheet).Range(p_deletePlace).Select
            Selection.EntireRow.Delete
        Wend
    End With
    With Worksheets("QUERY")
        '在query里不为空且没有达到最后一行,最后一行结束的标记为"结束"
        While .Cells(p_queryRows, p_queryCol).Value <> "" And .Cells(p_queryRows, p_queryCol).Value <> "结果"

            '报表里插入新行
             Worksheets(p_ContentSheet).Rows(p_conRows).insert Shift:=xlUp
             '改变颜色
            Worksheets(p_ContentSheet).Rows(p_conRows).Select
             With Selection.Interior
                .Color = RGB(255, 255, 255)
            End With
            '复制linesToReplace列
            For Count = 0 To p_linesToReplace - 1
                  Worksheets(p_ContentSheet).Cells(p_conRows, p_conCol + Count).Value = .Cells(p_queryRows, p_queryCol + Count).Value
            Next Count
            '更新要复制的行号
            p_queryRows = p_queryRows + 1
        Wend
    End With
    '引用合计结果
    Worksheets(p_ContentSheet).Range(p_deletePlace).Offset(-1, 3).Value = _
        Worksheets("QUERY").Cells(p_queryRows, p_queryCol + 3).Value
    Worksheets(p_ContentSheet).Range(p_deletePlace).Offset(-1, 4).Value = _
        Worksheets("QUERY").Cells(p_queryRows, p_queryCol + 3).Value
    Worksheets(p_ContentSheet).Range(p_deletePlace).Offset(-1, 5).Value = _
        Worksheets("QUERY").Cells(p_queryRows, p_queryCol + 3).Value

End Sub

Sub Main()
    insert 18, 3, 8, 4, "转让及无偿划出企业(资产)情况表", "D8", 8
    insert 22, 3, 13, 4, "转让及无偿划出企业(资产)情况表", "D13", 8
End Sub

又修改了下,如下,减少了一个参数

 Option Explicit
 Public insertNum As Long


'p_queryRows:query起始行位置
'p_queryCol: query起始列位置
'p_conRows:workbook起始行位置
'p_conCol:workbook起始列位置
'p_ContentSheet: 报表的sheet名称
'p_linesToReplace: 要被替换的列数

 Sub insert(p_queryRows As Long, p_queryCol As Long, p_conRows As Long, p_conCol As Long, _
            p_ContentSheet As String, p_linesToReplace As Integer)
   
     Dim Count As Long                       '复制的列数
    '先将数据全部清除
    '选中要删除的区间C_deltePlace
      With Worksheets(p_ContentSheet).Cells(p_conRows, p_conCol)
        While Worksheets(p_ContentSheet).Cells(p_conRows, p_conCol).Value <> ""   '根据空行判断是否删除
            '选中删除
            Worksheets(p_ContentSheet).Cells(p_conRows, p_conCol).Select
            Selection.EntireRow.Delete
        Wend
    End With
   
    With Worksheets("QUERY")
        '在query里不为空且没有达到最后一行,最后一行结束的标记为"结束"
        While .Cells(p_queryRows, p_queryCol).Value <> "" And .Cells(p_queryRows, p_queryCol).Value <> "结果"

            '报表里插入新行
             Worksheets(p_ContentSheet).Rows(p_conRows).insert Shift:=xlUp
            
             '改变颜色
            Worksheets(p_ContentSheet).Rows(p_conRows).Select
             With Selection.Interior
                .Color = RGB(255, 255, 255)
            End With
           
            '复制linesToReplace列
            For Count = 0 To p_linesToReplace - 1
                  Worksheets(p_ContentSheet).Cells(p_conRows, p_conCol + Count).Value = .Cells(p_queryRows, p_queryCol + Count).Value
            Next Count
            '更新赋值的行号
            insertNum = insertNum + 1
           
            '更新要复制的行号
            p_queryRows = p_queryRows + 1
        Wend
    End With
   
    '引用合计结果
    Worksheets(p_ContentSheet).Cells(p_conRows, p_conCol).Offset(-1, 3).Value = _
        Worksheets("QUERY").Cells(p_queryRows, p_queryCol + 3).Value
    Worksheets(p_ContentSheet).Cells(p_conRows, p_conCol).Offset(-1, 4).Value = _
        Worksheets("QUERY").Cells(p_queryRows, p_queryCol + 3).Value
    Worksheets(p_ContentSheet).Cells(p_conRows, p_conCol).Offset(-1, 5).Value = _
        Worksheets("QUERY").Cells(p_queryRows, p_queryCol + 3).Value
   
End Sub

Sub Main()
    insertNum = 0

    insert 18, 3, 8, 4, "转让及无偿划出企业(资产)情况表", 8
    insert 18 + 1 + insertNum, 3, 8 + 2 + insertNum, 4, "转让及无偿划出企业(资产)情况表", 8
End Sub

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值