如何在下面公式
问题描述:
我要创建一个公式,其中说3列是目前A
提供范围在宏在Excel中,B
& C
其中有值,直到说串行5.所以,现在要检查如果单元格的值说C1
是空白的,那么单元格B1
将是A1/Count(C)
。如何在下面公式
我能执行此为单细胞,但如何可以使用下面的公式范围A1:A5
,B1:B5
和C1: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
答
放置到这个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
让我知道这是否工作。非常感谢!
答
我不确定究竟是什么公式插入,所以我把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/
按预期工作..谢谢! – user6284292
很高兴听到它。随时upvote答案,因此这个问题可以被关闭。您的upvote将允许我发表评论并在未来帮助更多的人。 – excelledsoftware
我试过但是不能这样做,至少需要15个声望才能这么做,而我的是1. – user6284292