VBA 汇总统计

Option Explicit


Sub sum_Click()
  Call loop_cells
End Sub


Sub loop_cells()
   Dim mysheet As Worksheet
   Set mysheet = ActiveWorkbook.Sheets(1)
 
   Dim r As Long
    
' 站点
   Dim cell_zd As String
' 雨量
   Dim cell_yl As Single
' 数据年月
   Dim cell_ny As String
   '
   Dim k_zd_ny As String
   
   
   Dim dict
   Set dict = CreateObject("Scripting.Dictionary")
   
   Dim dict_zhandian
   Set dict_zhandian = CreateObject("Scripting.Dictionary")
   
       
   For r = 2 To mysheet.UsedRange.Rows.Count
     cell_zd = mysheet.Cells(r, 1).Value
     cell_ny = CStr(mysheet.Cells(r, 2).Value) & "年" + CStr(mysheet.Cells(r, 3).Value) & "月"
     cell_yl = mysheet.Cells(r, 5).Value
     
     k_zd_ny = cell_zd + "_" + cell_ny
     
     If Not dict_zhandian.exists(cell_zd) Then
       dict_zhandian.Add (cell_zd), cell_yl
       dict.Add (k_zd_ny), cell_yl
     Else
       dict_zhandian.Item(cell_zd) = cell_yl + dict_zhandian.Item(cell_zd)
       dict.Item(k_zd_ny) = cell_yl + dict.Item(k_zd_ny)
     End If
   
   Next
      
  
   
   Dim st As Worksheet
   Set st = ActiveWorkbook.Sheets(2)
   st.Cells.ClearContents
      
   Dim k, v, k1, v1
   k = dict_zhandian.Keys
   v = dict_zhandian.Items
   k1 = dict.Keys
   v1 = dict.Items
   
   
   st.Cells(1, 1).Value = "站点名"
   st.Cells(1, 2).Value = "年降水(" + CStr(mysheet.Cells(3, 2).Value) + ")"
   
   
   Dim nMonth As Integer
   
   For nMonth = 1 To 12
    st.Cells(1, 2 + nMonth).Value = CStr(nMonth) & "月"
   Next
      
   Dim i As Integer
   For i = 0 To dict_zhandian.Count - 1
   
     st.Cells(i + 2, 1).Value = k(i)
     t.Cells(i + 2, 2).Value = v(i)
     
     For nMonth = 1 To 12
       st.Cells(i + 2, 2 + nMonth).Value = dict.Item(k(i) + "_" + CStr(mysheet.Cells(3, 2).Value) + "年" + CStr(nMonth) + "月")
     Next
     
          
   Next
   
   st.Activate
End Sub
 






下载地址: 下载
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值