2017-07-01 10 views
1

私はExcelのマクロを使い慣れていません。このマクロを入手するには問題があります。私はこのスクリプトを実行しようとすると、 "ランタイムエラー '13'タイプの不一致"エラーが発生します。私がしようとしているのは、セルが黒であることをExcelでチェックすることです.Excelに 'myBit'に対応する数値を 'ans'に追加し、行の最後に列 'J'を 'その後、次の列に対して繰り返されます。申し訳ありませんが、これが混乱していることがわかった場合、私は何をしようとしているのか説明しようと努力しました。 CellsためExcelマクロInterior.color

Private Sub CommandButton1_Click() 
Dim myRow 
Dim myBit 
Dim ans As Integer 
Dim c As Integer 
Dim r As Integer 
myRow = Array("B", "C", "D", "E", "F", "G", "H", "I") 
myBit = Array(128, 64, 32, 16, 8, 4, 2, 1) 
c = 0 
r = 3 
For r = 3 To 11 
    ans = 0 
    For c = 0 To 8 
     If Cells(myRow(c) + r).Interior.Color = RGB(0, 0, 0) Then ans = ans + myBit(c) 
    Cells("J" + r).Value = ans 
    Next c 
Next r 
End Sub 

答えて

7

構文はCells(Row,Column)です。あなたが代わりにそのRangeを必要とするこの

Worksheet.Cells Property

を参照してください。また、必要でないものはほとんどありません。これはあなたが試しているものですか(徹底的にテストされていません)?

Private Sub CommandButton1_Click() 
    Dim ans As Integer, c As Integer, r As Integer 
    Dim myRow, myBit 

    myRow = Array("B", "C", "D", "E", "F", "G", "H", "I") 
    myBit = Array(128, 64, 32, 16, 8, 4, 2, 1) 

    For r = 3 To 11 
     ans = 0 
     For c = 0 To 8 
      If Range(myRow(c) & r).Interior.Color = RGB(0, 0, 0) Then _ 
      ans = ans + Val(myBit(c)) 

      Range("J" & r).Value = ans 
     Next c 
    Next r 
End Sub 
+0

ありがとうございます!私は 'For'ループのコードを7の代わりに8を入れて訂正しましたが、これはすごくうまくいきます – Cabe

+1

元の質問に '8 'があるので' 8'を入れます:) –