Excel VBA 在保留原单元格数据的情况下,将计算的百分比加在后面

2020-08-13 15:35:26 浏览数 (1)

算的是红框占绿框的百分比

难点在保留原数据的情况下,把百分比加在后面。通过公式我是不会,但程序实验也不难。

搞定!

代码

代码语言:javascript复制
Attribute VB_Name = "模块1"
Sub cal1()
    Dim range1 As Range
    Set range1 = Application.InputBox("选择区域", Type:=8)
    x = range1.Rows.Count
    y = range1.Columns.Count
    
    Dim chushu As Integer
    Dim he As Integer
    Dim result As String
    Dim cut As Integer
    
    cut = 1
    
    For t = 1 To x
        If range1(t).Value = "" Then
            Exit For
        End If
        he = 0
        
        For i = 0 To y - 1
            he = he   Val(range1(cut   i))
        Next i
        
        For i = 0 To y - 1
            chushu = Val(range1(cut   i))
            result = chushu & "(" & Application.Round((chushu / he) * 100, 2) & "%)"
             range1(cut   i) = result
        Next i
        cut = cut   y
    Next t

    
End Sub

欢迎关注↓↓↓

  • 微信公众号:xfxuezhang

0 人点赞