首页 文章

如何在下面的公式中提供Excel中的宏范围

提问于
浏览
0

我要创建一个公式,其中说3列存在 ABC 其中有值,直到说串行5.所以,现在要检查如果单元格的值说 C1 是空白的,然后细胞 B1A1/Count(C) .

我可以为单个单元格执行此操作,但如何使用以下公式范围从 A1:A5B1:B5C1:C5

Sub CheckCnt()

Range("C6") = WorksheetFunction.CountA(Range("C1:C5"))

If Range("C1") = "" Then
    Range("B1") = WorksheetFunction.Round(Range("A1") / Range("C6"), 2)
Else
Range("B1") = 0

End If
End Sub

2 回答

  • 0

    将其置于FOR循环中应该有效 . 请参阅下面的修订代码 .

    Sub CheckCnt()
        Dim rw as Integer
    
    Range("C6").Value = WorksheetFunction.CountA(Range("C1:C5"))
    For rw = 1 to 5
        If Range("C" & rw).Value = "" Then
            Range("B" & rw).Value = WorksheetFunction.Round(Range("A" & rw).Value / Range("C6").Value, 2)
        Else
            Range("B" & rw).Value = 0
        End If
    Next rw
    
    End Sub
    

    让我知道这个是否奏效 . 非常感谢!

  • 0

    我不确定究竟要插入哪个公式,所以我将ROUND公式放在B1:B5范围内 .

    B1将是: =ROUND($A1/$C$6,2)
    B2将是: =ROUND($A2/$C$6,2)
    等等...

    此代码使用R1C1表示法:

    Sub CheckCnt()
    
        'Be specific about which sheet you want the result in (otherwise it will appear in the selected sheet).
        With ThisWorkbook.Worksheets("Sheet1")
            With .Range("B1:B5")
                .FormulaR1C1 = "=Round(RC1/R6C3,2)" 'Use a formula with R1C1 notation.
                .Value = .Value 'Replace the formula with the result of the formula.
            End With
        End With
    
    End Sub
    

    RC1 表示此行第1列. R6C3 表示第6行第3列 .
    http://www.numeritas.co.uk/2013/09/the-%E2%80%98dark-art%E2%80%99-of-r1c1-notation/

相关问题