Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1941|回复: 6

请问隔列求和用VBA怎么实现呢?

[复制链接]
发表于 2020-3-11 17:18 | 显示全部楼层 |阅读模式
每行的“周和”=它前面的7个单元格之和,因求和单元格非常多,所以考虑用VBA,谢谢!

2020.zip

29.81 KB, 下载次数: 15

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2020-3-11 18:19 | 显示全部楼层
点左上角的按钮运行。运行完成后,最好移动或复制工作表到新文件,另存。

2020.rar

78.67 KB, 下载次数: 17

回复

使用道具 举报

发表于 2020-3-11 19:44 | 显示全部楼层
Sub test()
    Dim i, j, ar, s
    r = ActiveSheet.UsedRange.Rows.Count
    l = ActiveSheet.UsedRange.Columns.Count
    ar = Range(Cells(3, 1), Cells(r, l)).Value
    For i = 2 To UBound(ar)
       For j = 2 To UBound(ar, 2)
          s = s + ar(i, j)
          If ar(1, j) = "周和" Then
             s = s - ar(i, j)
             ar(i, j) = ""
             If s <> 0 Then
                ar(i, j) = s
                s = 0
             End If
          End If
       Next
    Next
    Range("a3").Resize(UBound(ar), UBound(ar, 2)) = ar
End Sub
回复

使用道具 举报

发表于 2020-3-11 20:22 | 显示全部楼层
  1. Sub test()
  2. Dim arr, j&, rng As Range
  3. arr = Range("a3", Cells(3, Columns.Count).End(1))
  4. For j = 2 To UBound(arr, 2)
  5.   If arr(1, j) = "周和" Then
  6.     If rng Is Nothing Then Set rng = Cells(4, j).Resize(30) Else Set rng = Union(rng, Cells(4, j).Resize(30))
  7.   End If
  8. Next j
  9. rng = "=sum(RC[-7]:RC[-1])"
  10. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2020-3-12 08:19 | 显示全部楼层

谢谢版主的回复!
回复

使用道具 举报

 楼主| 发表于 2020-3-12 08:20 | 显示全部楼层
scl5801 发表于 2020-3-11 19:44
Sub test()
    Dim i, j, ar, s
    r = ActiveSheet.UsedRange.Rows.Count

谢谢回复
回复

使用道具 举报

发表于 2020-3-12 09:27 | 显示全部楼层

单元格怎样用VBA直接引用公式,老师又教了一招!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-3-29 20:03 , Processed in 0.403896 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表