简体   繁体   English

Excel宏Interior.color

[英]Excel macro Interior.color

I'm new to Excel macros and I am having some trouble getting this macro I wrote to work. 我是Excel宏的新手,但在使我编写的该宏开始工作时遇到了一些麻烦。 When I try to run this script I get a "Runtime Error '13' Type mismatch" error. 当我尝试运行此脚本时,出现“运行时错误'13'类型不匹配”错误。 What I am trying to do is have excel check is a cell is black, then if it is, have Excel add the corresponding number in 'myBit' to 'ans' and at the end of the row, set column 'J' to 'ans' and then repeated for the next column. 我想做的是让excel检查一个单元格是否为黑色,如果是,请让Excel在“ myBit”中将相应的数字添加到“ ans”,并在该行的末尾,将“ J”列设置为“ ans”,然后在下一列重复。 Sorry if you found this confusing but I did my best at trying to explain what I am trying to do. 抱歉,如果您发现这令人困惑,但是我尽力解释了我要做什么。

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

The syntax for Cells is Cells(Row,Column) . Cells的语法是Cells(Row,Column) Please see this 请看这个

Worksheet.Cells Property Worksheet.Cells属性

You need Range instead of that. 您需要Range而不是那个。 Also few things are not required. 也不需要几件事。 Is this what you are trying ( Not tested thoroughly )? 这是您要尝试的( 未完全测试 )吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM