单元格颜色条件循环。对象必需的错误

时间:2014-10-24 11:51:44

标签: vba colors conditional-statements do-while

我正在尝试创建两个数组:一个具有橙色单元格的行数,另一个具有蓝色单元格的行数。我一直试图调试这段代码但是它给了我错误:"编译错误:需要对象&#34 ;;同时以黄色突出显示函数的第一行:"函数ArrayOrangeBlue()"。我是VBA的新手,我很确定我在语法中遗漏了一些东西。

您有任何意见吗?

Sub CommandButton1_Clicked()
    ArrayOrangeBlue
End Sub

Function ArrayOrangeBlue()

Dim i As Integer    'row number'
Dim j As Integer    'orange counter'
Dim k As Integer    'blue counter'

Dim l As Integer
Dim m As Integer

Dim blue(1 To 1000) As Double
Dim orange(1 To 1000) As Double

'Starting Row'
Set i = 10

'Initialize orange and blue counters to 1'
Set j = 1
Set k = 1

Set l = 10
Set m = 10


'Loop until Row 1000'
Do While i <= 1000

'Dim cell As Range

'Set cell = ActiveSheet.Cells(i, 1)

    'If cell colour is Orange- note absolute row number (i) in array: orange'
    If Cells(i, 1).Interior.Color = 9420794 Then

        orange(j) = i
        Sheets("Detail analysis").Cells(l, 15) = i
        j = j + 1
        l = l + 1
        'MsgBox ("This one is Orange")

    Else

        'If cell colour is Blue- note absolute row number (i) in array: blue'
        If Cells(i, 1).Interior.Color = 13995347 Then

        blue(k) = i
        Sheets("Detail analysis").Cells(m, 16) = i
        k = k + 1
        m = m + 1
        'MsgBox ("This one is Blue")

        End If

    End If

    i = i + 1
Loop


End Function

1 个答案:

答案 0 :(得分:0)

所以最后我设法解决了我的代码,找到并注意了某些颜色的单元格。

此处代码将橙色单元格的行号存储在数组orange()和数组Arr()的第1列中,蓝色单元格存储在数组blue()和数组Arr()的第2列中。

初始化变量时删除了“Set”命令。

然而,更重要的是,我被要求在每个If语句中指定表单。

我希望这有助于其他人。

'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ArrayOrangeBlue Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'

Dim i As Integer    ' row number '
Dim j As Integer    ' orange counter '
Dim k As Integer    ' blue counter '

Dim l As Integer
Dim m As Integer

Dim blue(1 To 50) As Double
Dim orange(1 To 50) As Double
Dim green As Double


' Starting Row '

i = 10

' Initialize orange and blue counters to 1 '

j = 1
k = 1



l = 10
m = 10
Dim Arr(100, 2) As Double

' Loop until Row 1000 '

Do While i <= 1000


''''' If cell colour is Orange- note absolute row number (i) in array: orange '

If Sheets("Detail analysis").Cells(i, 1).Interior.Color = 9420794 Then

    orange(j) = i
    Arr(j, 1) = i


    j = j + 1

Else

''''''''' If cell colour is Blue- note absolute row number (i) in array: blue '

    If Sheets("Detail analysis").Cells(i, 1).Interior.Color = 13995347 Then

    blue(k) = i
    Arr(k, 2) = i


    k = k + 1

    Else

''''''''''''' If cell colour is Gren- store the absolute row number (i) in: green '

        If Sheets("Detail analysis").Cells(i, 1).Interior.Color = 5296274 Then

        Arr(j, 1) = i
        green = i

        End If
    End If
End If

i = i + 1
Loop