アプリ版:「スタンプのみでお礼する」機能のリリースについて

ExcelのVBAマクロで、For ~ Then構文で複数のセルを参照していき、
色付きのセル(塗りなしのセル)に入力されている値のみを取得する方法を教えてください。
Excel2007を使用しています。

A 回答 (3件)

チカラワザで一つずつ巡回して調べていくのが一番簡単です。



sub macro1()
 dim h as range
 dim res1 as double, res2 as double

 on error resume next
 for each h in selection
  if h.interior.colorindex = xlnone then
   res1 = res1 + h.value
  else
   res2 = res2 + h.value
  end if
 next

 msgbox "塗り無し合計 " & res1
 msgbox "塗りあり合計 " & res2
end sub

「値のみ取得」したのを具体的にどうしたいのかご相談に書かれていませんが、自力で適切に応用してみて下さい。
    • good
    • 1

セルに、ユーザー定義関数としても、関数のまま置くことが可能ですが、以下は、マクロで使った方がよいです。



'//
Function ColorSum(rng As Range, Optional bln = False)
'rng は、セルの範囲, bln は、False デフォルト、色付きのセル, True, 色なしのセル
 Dim cSum As Double
 Dim nSum As Double
 Dim c As Range
 For Each c In rng
 If VarType(c.Value) = vbDouble Then
  If c.Interior.ColorIndex <> xlColorIndexNone Then
   cSum = cSum + c.Value
  Else
   nSum = nSum + c.Value
  End If
 End If
 Next c
If bln Then
  ColorSum = nSum
Else
  ColorSum = cSum
End If
End Function

'例:
Sub Test01()
  Dim r As Range
  Set r = Range("A1", Cells(Rows.Count, 1).End(xlUp))

  r.Cells(r.Count + 1, 1).Value = ColorSum(r)
  r.Cells(r.Count + 2, 1).Value = ColorSum(r, True)
End Sub
'//
    • good
    • 2
この回答へのお礼

参考になりました。ありがとうございます。

お礼日時:2015/01/12 01:33

こちらが参考になるかと。



参考URL:http://hp.vector.co.jp/authors/VA016119/hajimete …
    • good
    • 0
この回答へのお礼

このような方法もあるのですね。ありがとうございました。

お礼日時:2015/01/12 01:33

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

このQ&Aを見た人はこんなQ&Aも見ています


このQ&Aを見た人がよく見るQ&A