Excel VBA 在保留原单元格数据的情况下,将计算的百分比加在后面
算的是红框占绿框的百分比
难点在保留原数据的情况下,把百分比加在后面。通过公式我是不会,但程序实现也不难。
先在Excel中的开发工具中打开visual basic,或者用宏也可以
导入代码文件,代码在最后
导入后点击绿色三角形,运行即可
选择需要计算的区域
点击确定后自动完成计算并修改
搞定!
代码
1 Attribute VB_Name = "模块1" 2 Sub cal1() 3 Dim range1 As Range 4 Set range1 = Application.InputBox("选择区域", Type:=8) 5 x = range1.Rows.Count 6 y = range1.Columns.Count 7 8 Dim chushu As Integer 9 Dim he As Integer 10 Dim result As String 11 Dim cut As Integer 12 13 cut = 1 14 15 For t = 1 To x 16 If range1(t).Value = "" Then 17 Exit For 18 End If 19 he = 0 20 21 For i = 0 To y - 1 22 he = he + Val(range1(cut + i)) 23 Next i 24 25 For i = 0 To y - 1 26 chushu = Val(range1(cut + i)) 27 result = chushu & "(" & Application.Round((chushu / he) * 100, 2) & "%)" 28 range1(cut + i) = result 29 Next i 30 cut = cut + y 31 Next t 32 33 34 End Sub
欢迎关注↓↓↓
-
头条号:小锋学长
-
微信公众号:xfxuezhang