Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 单元格颜色条件循环。对象要求错误_Vba_Colors_Conditional Statements_Do While - Fatal编程技术网

Vba 单元格颜色条件循环。对象要求错误

Vba 单元格颜色条件循环。对象要求错误,vba,colors,conditional-statements,do-while,Vba,Colors,Conditional Statements,Do While,我试图创建两个数组:一个是橙色单元格的行号,另一个是蓝色单元格的行号。我试着调试这段代码有一段时间了,但它给了我一个错误:编译错误:objectrequired;同时以黄色突出显示函数的第一行:函数数组蓝色。我是VBA新手,我很确定我在语法上遗漏了一些东西 你有什么反馈吗 Sub CommandButton1_Clicked() ArrayOrangeBlue End Sub Function ArrayOrangeBlue() Dim i As Integer 'row nu

我试图创建两个数组:一个是橙色单元格的行号,另一个是蓝色单元格的行号。我试着调试这段代码有一段时间了,但它给了我一个错误:编译错误:objectrequired;同时以黄色突出显示函数的第一行:函数数组蓝色。我是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

因此,最后我设法解析了我的代码,找到并记录具有特定颜色的单元格

在这里,代码将橙色单元格的行号存储在数组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
从包含变量赋值行(如集合j=1)的行中省略集合