電子書籍の厳選無料作品が豊富!

先ほどの続きですが、これではすべて加算してしまいます。

Function SumColoredCells(pRange As Range) As Double
Dim cel As Range
Dim total As Double
For Each cel In pRange
If cel.Interior.ColorIndex <> 0 Then
total = total + cel.Value
End If
Next cel
SumColoredCells = total
End Function

質問者からの補足コメント

  • うーん・・・

    皆さんありがとうございます。うまく計算できますが、色を付けたり消したりすると計算は自動更新しないのですが、この対応はどうしたらよいのでしょうか?。初歩的な事で誠にすいません。

      補足日時:2023/02/28 15:35

A 回答 (4件)

多用すると重くなりますが、application.volatileを追加することで


再計算してくれます。
https://learn.microsoft.com/ja-jp/office/vba/api …
https://www.excel-chunchun.com/entry/worksheet-f …
Function直下に置くことで、ユーザー関数に意味を持たせます。

Function SumColoredCells(pRange As Range) As Double
Application.Volatile
Dim cel As Range
Dim total As Double
 For Each cel In pRange
  If cel.Interior.ColorIndex <> xlNone Then
   total = total + cel.Value
  End If
 Next cel
SumColoredCells = total
End Function
    • good
    • 0
この回答へのお礼

大変お世話になりました。本当にありがとうございました。感謝

お礼日時:2023/02/28 16:09

No.1の回答にある-4142は、定数xlNone を値にしたもの。


https://learn.microsoft.com/ja-jp/office/vba/api …
結局は同じものなので、保存先さえ間違えなければ使えたはず。
    • good
    • 0

先ほど回答したものですが、無視ですか?


Excel2013で確認したので、元のユーザー関数のままで可能です。

Function SumColoredCells(pRange As Range) As Double
Dim cel As Range
Dim total As Double
 For Each cel In pRange
  If cel.Interior.ColorIndex <> xlNone Then
   total = total + cel.Value
  End If
 Next cel
SumColoredCells = total
End Function

保存先を標準モジュールにするだけです。
「excel2013 色付きセルの値合計」の回答画像2
    • good
    • 0

0ではなく-4142にしてみてはいかがでしょう。

    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!